当前位置: 首页 > news >正文

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开发技巧,觉得有用记得一键三连哦。

相关文章:

  • 语言革命:NLP与GPT-3.5如何改变我们的世界
  • collection、ofType、select的联合用法(Mybatis实现树状结构查询)
  • 【极数系列】Flink环境搭建Linux版本 (03)
  • linux 云主机扩展磁盘的步骤
  • win11 蓝屏分析,没有一定的原因,没有解决办法,只能不断尝试
  • 常用命令-
  • 内置函数和推导式
  • 83.网游逆向分析与插件开发-背包的获取-自动化助手显示装备数据
  • FPGA硬件架构
  • 第26讲:顺序表的应用(通讯录)
  • 人工智能与机器学习——开启智能时代的里程碑
  • JavaEE-自定义SSM-编写核心- my spring bean工厂(IoC、DI)
  • MySQL库表操作 作业
  • C语言基础13
  • HarmonyOS 鸿蒙应用开发 (七、HTTP网络组件 axios 介绍及封装使用)
  • [ JavaScript ] 数据结构与算法 —— 链表
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • css选择器
  • echarts的各种常用效果展示
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • export和import的用法总结
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • rabbitmq延迟消息示例
  • SQLServer之创建数据库快照
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 理解在java “”i=i++;”所发生的事情
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 如何利用MongoDB打造TOP榜小程序
  • 入手阿里云新服务器的部署NODE
  • 使用docker-compose进行多节点部署
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 用简单代码看卷积组块发展
  • 智能合约Solidity教程-事件和日志(一)
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • #LLM入门|Prompt#3.3_存储_Memory
  • #宝哥教你#查看jquery绑定的事件函数
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (2)STM32单片机上位机
  • (3)nginx 配置(nginx.conf)
  • (C语言)共用体union的用法举例
  • (poj1.2.1)1970(筛选法模拟)
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (理论篇)httpmoudle和httphandler一览
  • (七)Knockout 创建自定义绑定
  • (三)Honghu Cloud云架构一定时调度平台
  • (三)mysql_MYSQL(三)
  • (四)库存超卖案例实战——优化redis分布式锁
  • (转)Sublime Text3配置Lua运行环境
  • .NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态
  • .net6 webapi log4net完整配置使用流程
  • .net访问oracle数据库性能问题
  • /dev/VolGroup00/LogVol00:unexpected inconsistency;run fsck manually