Unity3D正则表达式的使用

系列文章目录

unity工具


文章目录

  • 系列文章目录
  • 前言
  • 一、匹配正整数的使用方法
    • 1-1、代码如下
    • 1-2、结果如下
  • 二、匹配大写字母
    • 2-1、代码如下
    • 1-2、结果如下
  • 三、Regex类
    • 3-1、Match()
    • 3-2、Matches()
    • 3-3、IsMatch()
  • 四、定义正则表达式
    • 4-1、转义字符
    • 4-2、字符类
    • 4-3、定位点
    • 4-4、限定符
  • 五、常用的正则表达式
    • 5-1、校验数字的表达式
    • 5-2、校验字符的表达式
    • 5-3、校验特殊需求的表达式
  • 六、正则表达式实例
    • 6-1、匹配字母的表达式
    • 6-2、替换掉空格的表达式
  • 七、完整的测试代码
  • 总结


在这里插入图片描述

前言

大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。
正则表达式,又称规则表达式,在代码中常简写为regex、regexp,常用来检索替换那些符合某种模式的文本。
许多程序设计语言都支持利用正则表达式进行字符串操作。


提示:以下是本篇文章正文内容,下面案例可供参考

unity使用正则表达式

一、匹配正整数的使用方法

1-1、代码如下

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
/// <summary>
/// 匹配正整数
/// </summary>
public class Bool_Number : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){string num = "456";Debug.Log("结果是:"+IsNumber(num));}public bool IsNumber(string strInput){Regex reg = new Regex("^[0-9]*[1-9][0-9]*$");if (reg.IsMatch(strInput)){return true;}else{return false;}}
}

1-2、结果如下

在这里插入图片描述

二、匹配大写字母

检查文本是否都是大写字母

2-1、代码如下

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
/// <summary>
/// 匹配大写字母
/// </summary>
public class Bool_Majuscule : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){string NUM = "ABC";Debug.Log("NUM结果是:" + IsCapital(NUM));string num = "abc";Debug.Log("num结果是:" + IsCapital(num));}public bool IsCapital(string strInput){Regex reg = new Regex("^[A-Z]+$");if (reg.IsMatch(strInput)){return true;}else{return false;}}
}

1-2、结果如下

在这里插入图片描述

三、Regex类

正则表达式是一种文本模式,包括普通字符和特殊字符,正则表达式使用单个字符描述一系列匹配某个句法规则的字符串 常用方法如下在这里插入图片描述
如需了解更详细文档请参考:https://www.runoob.com/csharp/csharp-regular-expressions.html

3-1、Match()

测试代码如下

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;public class Bool_Regex_Match : MonoBehaviour
{void Start(){string temp = "aaaa(bbb)cccccc(dd)eeeeee";IsMatch(temp);}///<summary>///在输入的字符串中搜索正则表达式的匹配项///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch(string strInput){string pattern = "\\(\\w+\\)";Match result = Regex.Match(strInput, pattern);Debug.Log("第一种重载方法:" + result.Value);Match result2 = Regex.Match(strInput, pattern, RegexOptions.RightToLeft);Debug.Log("第二种重载方法:" + result2.Value);}}

测试结果
在这里插入图片描述

3-2、Matches()

代码如下:

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;public class Bool_Regex_Matches : MonoBehaviour
{void Start(){string temp = "aaaa(bbb)aaaaaaaaa(bb)aaaaaa";IsCapital(temp);}///<summary>///在输入的字符串中搜索正则表达式的匹配项///</summary>///<param name="strInput">输入的字符串</param>public void IsCapital(string strInput){string pattern = "\\(\\w+\\)";MatchCollection results = Regex.Matches(strInput, pattern);for (int i = 0; i < results.Count; i++){Debug.Log("第一种重载方法:" + results[i].Value);}MatchCollection results2 = Regex.Matches(strInput, pattern, RegexOptions.RightToLeft);for (int i = 0; i < results.Count; i++){Debug.Log("第二种重载方法:" + results2[i].Value);}}
}

结果如下
在这里插入图片描述

3-3、IsMatch()

代码如下:

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;public class Bool_Regex_IsMatch : MonoBehaviour
{void Start(){string temp = "aaaa(bbb)cccccc(dd)eeeeeeee";IsMatch(temp);}///<summary>///在输入的字符串中搜索正则表达式的匹配项///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch(string strInput){string pattern = "\\(\\w+\\)";bool resultBool = Regex.IsMatch(strInput, pattern);Debug.Log(resultBool);bool resultBool2 = Regex.IsMatch(strInput, pattern, RegexOptions.RightToLeft);Debug.Log(resultBool2);}
}

结果如下
在这里插入图片描述

四、定义正则表达式

4-1、转义字符

总结:在这里插入图片描述

方法如下:

  ///<summary>///在输入的字符串中搜索正则表达式的匹配项  (转义字符)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch(string strInput){string pattern = "\\r\\n(\\w+)";Match resultBool = Regex.Match(strInput, pattern);Debug.Log(resultBool.Value);}

4-2、字符类

总结:
在这里插入图片描述

方法如下:

  ///<summary>///在输入的字符串中搜索正则表达式的匹配项 (字符类)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_1(string strInput){string pattern = "(\\d+)";Match resultBool = Regex.Match(strInput, pattern);Debug.Log(resultBool.Value);}

4-3、定位点

正则表达式中的定位点可以设置匹配字符串的索引位置,所以可以使用定位点对要匹配的字符进行限定,以此得到想要匹配到的字符串
总结:在这里插入图片描述
方法代码如下:

 ///<summary>///在输入的字符串中搜索正则表达式的匹配项 (定位点)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_2(string strInput){string pattern = "(\\w+)$";Match resultBool = Regex.Match(strInput, pattern);Debug.Log(resultBool.Value);}

4-4、限定符

正则表达式中的限定符指定在输入字符串中必须存在上一个元素的多少个实例才能出现匹配项
在这里插入图片描述
方法如下:

 ///<summary>///在输入的字符串中搜索正则表达式的匹配项  (限定符)///</summary> ///<param name="strInput">输入的字符串</param>public void IsMatch_3(string strInput){string pattern = "\\w{5}";Match resultBool = Regex.Match(strInput, pattern);Debug.Log(resultBool.Value);}

五、常用的正则表达式

5-1、校验数字的表达式

代码如下:

  ///<summary>///在输入的字符串中搜索正则表达式的匹配项  (常用的校验数字表达式)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_4(string strInput){Regex reg = new Regex(@"^[0-9]*$");bool result = reg.IsMatch(strInput);Debug.Log(result);}

5-2、校验字符的表达式

字符如果包含汉字 英文 数字以及特殊符号时,使用正则表达式可以很方便的将这些字符匹配出来
代码如下:

///<summary>///在输入的字符串中搜索正则表达式的匹配项  (常用的校验字符表达式)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_5(string strInput){Regex reg = new Regex(@"^[\u4E00-\u9FA5A-Za-z0-9]");bool result = reg.IsMatch(strInput);Debug.Log("匹配中文、英文和数字:" + result);Regex reg2 = new Regex(@"^[A-Za-z0-9]");bool result2 = reg2.IsMatch(strInput);Debug.Log("匹配英文和数字:" + result2);}

5-3、校验特殊需求的表达式

方法如下:

 ///<summary>///在输入的字符串中搜索正则表达式的匹配项  (常用的校验特殊需求的表达式)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_6(){Regex reg = new Regex(@"[a-zA-z]+://[^\s]*");bool result = reg.IsMatch("http://www.baidu.com");Debug.Log("匹配网址:" + result);Regex reg2 = new Regex(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");bool result2 = reg2.IsMatch("13512341234");Debug.Log("匹配手机号码:" + result2);}

六、正则表达式实例

(经常用到的)

6-1、匹配字母的表达式

开发中经常要用到以某个字母开头或某个字母结尾的单词

下面使用正则表达式匹配以m开头,以e结尾的单词
代码如下:

///<summary>///在输入的字符串中搜索正则表达式的匹配项  (匹配以m开头,以e结尾的表达式)///</summary>///<param name="strInput">输入的字符串</param>public void MatchStr(string str){Regex reg = new Regex(@"\bm\S*e\b");MatchCollection mat = reg.Matches(str);foreach (Match item in mat){Debug.Log(item);}}

6-2、替换掉空格的表达式

项目中,总会遇到模型名字上面有多余的空格,有时候会影响查找,所以下面演示如何去掉多余的空格
代码如下:

  ///<summary>///在输入的字符串中搜索正则表达式的匹配项  (去掉多余的空格的表达式)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_8(string str){Regex reg = new Regex("\\s+");Debug.Log(reg.Replace(str, " "));}

七、完整的测试代码

代码如下:

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;public class Bool_Definition : MonoBehaviour
{void Start(){#region 转义字符测试string temp = "\r\nHello\nWorld.";IsMatch(temp);#endregion#region 字符类测试string temp1 = "Hello World 2024";IsMatch_1(temp1);#endregion#region 定位点测试string temp2 = "Hello World 2024";IsMatch_2(temp2);#endregion#region 限定符测试string temp3 = "Hello World 2024";IsMatch_3(temp3);#endregion#region 校验数字测试string temp4 = "2024";IsMatch_4(temp4);#endregion#region 校验字符测试string temp5 = "你好,时间,2024";IsMatch_5(temp5);#endregion#region 校验特殊需求测试      IsMatch_6();#endregion#region 匹配字母实例测试    string temp7 = "make mave move and to it mease";IsMatch_7(temp7);#endregion#region 去掉空格实例测试    string temp8 = "GOOD     2024";IsMatch_8(temp8);#endregion}///<summary>///在输入的字符串中搜索正则表达式的匹配项  (转义字符)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch(string strInput){string pattern = "\\r\\n(\\w+)";Match resultBool = Regex.Match(strInput, pattern);Debug.Log(resultBool.Value);}///<summary>///在输入的字符串中搜索正则表达式的匹配项 (字符类)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_1(string strInput){string pattern = "(\\d+)";Match resultBool = Regex.Match(strInput, pattern);Debug.Log(resultBool.Value);}///<summary>///在输入的字符串中搜索正则表达式的匹配项 (定位点)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_2(string strInput){string pattern = "(\\w+)$";Match resultBool = Regex.Match(strInput, pattern);Debug.Log(resultBool.Value);}///<summary>///在输入的字符串中搜索正则表达式的匹配项  (限定符)///</summary> ///<param name="strInput">输入的字符串</param>public void IsMatch_3(string strInput){string pattern = "\\w{5}";Match resultBool = Regex.Match(strInput, pattern);Debug.Log(resultBool.Value);}///<summary>///在输入的字符串中搜索正则表达式的匹配项  (常用的校验数字表达式)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_4(string strInput){Regex reg = new Regex(@"^[0-9]*$");bool result = reg.IsMatch(strInput);Debug.Log(result);}///<summary>///在输入的字符串中搜索正则表达式的匹配项  (常用的校验字符表达式)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_5(string strInput){Regex reg = new Regex(@"^[\u4E00-\u9FA5A-Za-z0-9]");bool result = reg.IsMatch(strInput);Debug.Log("匹配中文、英文和数字:" + result);Regex reg2 = new Regex(@"^[A-Za-z0-9]");bool result2 = reg2.IsMatch(strInput);Debug.Log("匹配英文和数字:" + result2);}///<summary>///在输入的字符串中搜索正则表达式的匹配项  (常用的校验特殊需求的表达式)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_6(){Regex reg = new Regex(@"[a-zA-z]+://[^\s]*");bool result = reg.IsMatch("http://www.baidu.com");Debug.Log("匹配网址:" + result);Regex reg2 = new Regex(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");bool result2 = reg2.IsMatch("13512341234");Debug.Log("匹配手机号码:" + result2);}///<summary>///在输入的字符串中搜索正则表达式的匹配项  (匹配以m开头,以e结尾的表达式)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_7(string str){Regex reg = new Regex(@"\bm\S*e\b");MatchCollection mat = reg.Matches(str);foreach (Match item in mat){Debug.Log(item);}}///<summary>///在输入的字符串中搜索正则表达式的匹配项  (去掉多余的空格的表达式)///</summary>///<param name="strInput">输入的字符串</param>public void IsMatch_8(string str){Regex reg = new Regex("\\s+");Debug.Log(reg.Replace(str, " "));}}

总结

以后有更好用的会继续补充
不定时更新Unity开发技巧,觉得有用记得一键三连哦。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/441290.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【测试运维】接口测试各知识md文档学习笔记第1篇(已分享,附代码)

本系列文章md笔记&#xff08;已分享&#xff09;主要讨论接口测试相关知识。目标&#xff1a;了解接口测试服务对象&#xff0c;基本作用&#xff0c;接口测试环境插件工具&#xff0c;掌握jmeter的使用&#xff0c;掌握接口测试的测试用例编写。熟悉Jmeter工具组成&#xff0…

数据的绘画:数据可视化为我们打开信息的画卷

在迅猛发展的大数据时代&#xff0c;数据可视化成为我们逐步适应信息激增的得力工具。通过直观展示庞大复杂的数据&#xff0c;数据可视化不仅简化了信息的理解&#xff0c;更为我们提供了深入洞察的机会。这一技术的崛起不仅仅是技术的进步&#xff0c;更是对我们信息处理方式…

C++_list

目录 一、模拟实现list 1、list的基本结构 2、迭代器封装 2.1 正向迭代器 2.2 反向迭代器 3、指定位置插入 4、指定位置删除 5、结语 前言&#xff1a; list是STL(标准模板库)中的八大容器之一&#xff0c;而STL属于C标准库的一部分&#xff0c;因此在C中可以直接使用…

04MARL - priori kownledge and challenge

文章目录 一、General Learning Process二、中心化学习与独立学习1.central learning1.1 CQL1.2 局限性 2.Independent Learning2.1 IQL2.2 局限性 三、MARL挑战1.非平稳性1.1单智能体强化学习的非平稳性1.2 MARL非平稳性 2.信用分配问题3.均衡选择4.拓展性 四、协作型MARL的模…

Web实战丨基于Django的简单网页计数器

文章目录 写在前面Django简介主要程序运行结果系列文章写在后面 写在前面 本期内容 基于django的简单网页计数器 所需环境 pythonpycharm或vscodedjango 下载地址 https://download.csdn.net/download/m0_68111267/88795604 Django简介 Django 是一个用 Python 编写的高…

MySQL的原生API实现插入数据后在可视化工具上不显示的问题解决

显示表中有两行数据&#xff0c;该表也设置了主键和唯一索引 点进表里看却没有数据 问题原因出现在这里&#xff0c;虽然很多常用的数据库连接池都会开启自动提交&#xff0c;但ibatis的SqlSession使用sessionFactory.openSession()创建时&#xff0c;默认的自动提交是false&am…

纯静态微信小程序水果商城

首页代码 <view class"container"><!-- 轮播图 --><view class"swiper-container"><swiper class"screen-swiper" indicator-dots"true" circular"true" autoplay"true" interval"300…

数据结构——栈和队列(C语言)

栈种常见的数据结构&#xff0c;它用来解决一些数据类型的问题&#xff0c;那么好&#xff0c;我来带着大家来学习一下栈 文章目录 栈对栈的认识栈的模拟实现栈的练习方法一方法二 栈 对栈的认识 栈&#xff08;stack&#xff09;是限定只能在表的一端进行插入删除操作的线性…

VxTerm:SSH工具中的中文显示和乱码时的相关信息和一些基本的知识

当我们写的程序含有控制台(Console)输出时&#xff0c;如果输入内容包含中文时&#xff0c;我们一般需要知道下面的信息&#xff0c;才能正确的搞清楚怎么处理中文显示的问题&#xff1a; 1、实际程序或文件中的实际编码&#xff1a; Linux下的应用程序和文本文件&#xff0c;…

系统架构设计师-21年-下午题目

系统架构设计师-21年-下午题目 更多软考知识请访问 https://ruankao.blog.csdn.net/ 试题一必答&#xff0c;二、三、四、五题中任选两题作答 试题一 (25分) 说明 某公司拟开发一套机器学习应用开发平台&#xff0c;支持用户使用浏览器在线进行基于机器学习的智能应用开发…

matlab appdesigner系列-仪器仪表3-旋钮

旋钮&#xff0c;同过旋转显示特定的值 示例&#xff1a;模拟收音机调频 操作步骤&#xff1a; 1&#xff09;将旋钮、标签按钮拖拽到画布上&#xff0c;将标签文字修改为&#xff1a;欢迎收听&#xff0c;并将旋钮其数值范围改为90-107 2&#xff09;设置旋钮的回调函数 代…

15EG使用vivado2023.1建立hello world工程

1:打开软件建立工程 2:使用vivado创建设计模块并生成bit文件 3:导出硬件平台&#xff0c;使用vitis建立工程 4:使用vitis创建应用程序项目 5:硬件设置与调试 1:打开软件建立工程 打开VIVADO2023.1 创建一个新的工程 输入项目名称和地址&#xff0c;下面那个选项为是否…

力扣日记1.30【回溯算法篇】78. 子集

力扣日记&#xff1a;【回溯算法篇】78. 子集 日期&#xff1a;2023.1.30 参考&#xff1a;代码随想录、力扣 78. 子集 题目描述 难度&#xff1a;中等 给你一个整数数组 nums &#xff0c;数组中的元素 互不相同 。返回该数组所有可能的子集&#xff08;幂集&#xff09;。 …

CCF-CSP 202312-2 因子化简(Java、C++、Python)

文章目录 因子化简题目背景问题描述输入格式输出格式样例输入样例输出样例解释子任务 满分代码JavaCPython线性筛法 因子化简 题目背景 质数&#xff08;又称“素数”&#xff09;是指在大于 1 的自然数中&#xff0c;除了 1 和它本身以外不再有其他因数的自然数。 问题描述…

现货白银的交易策略包括哪些内容?如何适应策略?

交易策略被投资者视为做现货白银交易通向盈利之路的必备工具&#xff0c;但是投资者却不知道如何建立一个适合自己的交易策略&#xff0c;如果直接拿别人的交易策略过来&#xff0c;就犯了拿来主义的毛病&#xff0c;这样是不行的&#xff0c;下面我们就来简单讨论一下如何建立…

Linux操作系统概述

操作系统&#xff08;Operating System&#xff09;的定义 操作系统&#xff0c;是指直接管理系统硬件和资源&#xff08;如 CPU、内存和存储空间&#xff09;的软件。 操作系统的基本功能 ①统一管理计算机资源&#xff1a;处理器资源&#xff0c;IO设备资源&#xff0c;存储…

使用AXI GPIO IP核点亮led

本例程使用SD卡启动模式 创建工程模板在hello_world中已经介绍过了&#xff0c;这里直接从配置完zynq ip核开始 点击加号&#xff0c;搜索GPIO&#xff0c;双击添加AXI GPIO IP核 双击 GPIO IP 核进行设置&#xff0c;设置为输出模式&#xff0c;位宽为4&#xff0c;设置完成后…

超过1000种工具揭示了单细胞RNA数据分析的趋势

文章&#xff1a;Over 1000 tools reveal trends in the single-cell RNA-seq analysis landscape 杂志&#xff1a;Genome Biology 年份&#xff1a;2021 从 2016 年开始&#xff0c;scRNA-tools 数据库&#xff08;https://www.scrna-tools.org/&#xff09;不断收集单细胞转…

el-input 显示最大长度和已输入内容长度

效果如下图 多行文本框&#xff1a; 单行文本框&#xff1a; 需要设置 maxlength 和 show-word-limit 两个属性&#xff0c;在使用 maxlength 属性限制最大输入长度的同时&#xff0c;可通过设置 show-word-limit 属性来展示字数统计。 <el-inputtype"textarea&quo…

ElasticSearch面试题整理(持续更新)

1. Elasticsearch 中的倒排索引是什么&#xff1f; Elasticsearch 使用一种称为倒排索引的结构&#xff0c;ES中的倒排索引其实就是 lucene 的倒排索引&#xff0c;区别于传统的正向索引&#xff0c;倒排索引会再存储数据时将关键词和数据进行关联&#xff0c;保存到倒排表中&…