Unity3D正则表达式的使用

news2024/9/22 19:28:17

系列文章目录

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 update
    void 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 update
    void 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.coloradmin.cn/o/1421399.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

TypeScript 学习笔记(Day2)

「写在前面」 本文为 b 站黑马程序员 TypeScript 教程的学习笔记。本着自己学习、分享他人的态度&#xff0c;分享学习笔记&#xff0c;希望能对大家有所帮助。推荐先按顺序阅读往期内容&#xff1a; 1. TypeScript 学习笔记&#xff08;Day1&#xff09; 目录 3 TypeScript 常…

Java-并发高频面试题

1.说一下你对Java内存模型&#xff08;JMM&#xff09;的理解&#xff1f; 其实java内存模型是一种抽象的模型&#xff0c;具体来看可以分为工作内存和主内存。 JMM规定所有的变量都会存储再主内存当中&#xff0c;再操作的时候需要从主内存中复制一份到本地内存&#xff08;c…

面试题:MySQL数据库索引失效的10连问你学会了吗?

博主猫头虎的技术世界 &#x1f31f; 欢迎来到猫头虎的博客 — 探索技术的无限可能&#xff01; 专栏链接&#xff1a; &#x1f517; 精选专栏&#xff1a; 《面试题大全》 — 面试准备的宝典&#xff01;《IDEA开发秘籍》 — 提升你的IDEA技能&#xff01;《100天精通Golang》…

【2024.1.30练习】李白打酒加强版(25分)

题目描述 题目思路 在最多数据的情况下&#xff0c;有100个店100朵花&#xff0c;总情况为的天文数字&#xff0c;暴力枚举已经不可能实现&#xff0c;考虑使用动态规划解决问题。最后遇到的一定是花&#xff0c;所以思路更倾向于倒推。 建立二维数组&#xff0c;容易联想到为…

vxe-table从2.0升级到3.0,vxe-table-plugin-virtual-tree虚拟滚动失效

问题&#xff1a;系统一直使用的vxe-table2.0&#xff0c;vxe-table2.0不支持树的虚拟滚动&#xff0c;为了解决这个问题&#xff0c;引入了vxe-table-plugin-virtual-tree插件&#xff0c;现在系统vxe-table升级3.0&#xff0c;vxe-table-plugin-virtual-tree的虚拟滚动失效了…

【MySQL】学习如何通过DQL进行数据库数据的条件查询

&#x1f308;个人主页: Aileen_0v0 &#x1f525;热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 ​&#x1f4ab;个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-63IIm2s5sIhQfsfy {font-family:"trebuchet ms",verdana,arial,sans-serif;font-siz…

2024年【T电梯修理】及T电梯修理复审模拟考试

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 T电梯修理是安全生产模拟考试一点通总题库中生成的一套T电梯修理复审模拟考试&#xff0c;安全生产模拟考试一点通上T电梯修理作业手机同步练习。2024年【T电梯修理】及T电梯修理复审模拟考试 1、【多选题】工作结束跨…

Windows驱动开发之环境搭建,长期Waiting for connecting...思路

Windows驱动开发之环境搭建 1、前期准备 Vmware虚拟机软件 Windows10 iso安装包 Visual Studio2022 IDE软件 SDK安装&#xff08;一定要勾选上debug选项&#xff0c;windbg在里面&#xff09; WDK&#xff08;Windows驱动程序工具包&#xff09; WDK安装请参考官方文档&…

人人都可配置的大屏可视化

大屏主要是为了展示数据和酷炫的效果&#xff0c;布局大部分是9宫格&#xff0c;或者在9宫格上做的延伸&#xff0c;现在介绍下 泛积木-低代码 提供的大屏可视化配置。 首先查看效果展示 泛积木-低代码大屏展示。 创建页面之后&#xff0c;点击进入编辑页面&#xff0c;在可视…

初识命令执行【简介、工作原理和利用方式】

★★免责声明★★ 文章中涉及的程序(方法)可能带有攻击性&#xff0c;仅供安全研究与学习之用&#xff0c;读者将信息做其他用途&#xff0c;由Ta承担全部法律及连带责任&#xff0c;文章作者不承担任何法律及连带责任。 0、前言 本文看完如果想对应实战相关内容&#xff0c;可…

openGauss学习笔记-211 openGauss 数据库运维-高危操作一览表

文章目录 openGauss学习笔记-211 openGauss 数据库运维-高危操作一览表211.1 禁止操作211.2 高危操作 openGauss学习笔记-211 openGauss 数据库运维-高危操作一览表 各项操作请严格遵守指导书操作&#xff0c;同时避免执行如下高危操作。 211.1 禁止操作 表1中描述在产品的操…

纯html+css+js静态汽车商城

首页代码 <!DOCTYPE html> <html class"no-js" lang"zxx"><head><meta charset"utf-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport" content&qu…

Datawhale 组队学习之大模型理论基础Task9 大模型法律

第11章 大模型法律 11.1 简介 此内容主要探讨法律对大型语言模型的开发和部署有何规定。 先看看法律的特点&#xff1a; 法律就如我国法律教材所给出的一样&#xff0c;有依靠国家强制力保证实施的特点。 而法律在大模型中也是不可或缺的&#xff0c;缺少了法律的约束&…

【复现】Ivanti Connect Secure命令注入漏洞(CVE-2024-21887)_33

目录 一.概述 二 .漏洞影响 三.漏洞复现 1. 漏洞一&#xff1a; 四.修复建议&#xff1a; 五. 搜索语法&#xff1a; 六.免责声明 一.概述 Ivanti Connect Secure&#xff08;9.x、22.x&#xff09;和 Ivanti Policy Secure&#xff08;9.x、22.x&#xff09;的 Web 组件…

java8 Duration类学习

Duration类 官网地址 基于时间的时间量&#xff0c;例如“34.5秒”。 此类以秒和纳秒为单位对时间的量或量进行建模。它可以使用其他基于持续时间的单位访问&#xff0c;如分钟和小时。此外&#xff0c;可以使用DAYS单位&#xff0c;并将其视为完全等于24小时&#xff0c;从…

如何利用故障根因分析快速定位故障原因?

「 背 景 」 众所周知&#xff0c;变更是线上环境不稳定的⾸要因素&#xff0c;有研究表明&#xff0c;线上70%的故障都是由某种变更⽽触发的。因此&#xff0c;当⽣产环境发⽣故障产⽣告警时&#xff0c;管理员第⼀直觉是怀疑近期是否发⽣过变更。此时&#xff0c;我们往往需…

Linux ---- Shell编程之正则表达式

一、正则表达式 ​ 由一类特殊字符及文本字符所编写的模式&#xff0c;其中有些字符&#xff08;元字符&#xff09;不表示字符字面意义&#xff0c;而表示控制或通配的功能&#xff0c;类似于增强版的通配符功能&#xff0c;但与通配符不同&#xff0c;通配符功能是用…

C++ 数论相关题目 博弈论:拆分-Nim游戏

给定 n 堆石子&#xff0c;两位玩家轮流操作&#xff0c;每次操作可以取走其中的一堆石子&#xff0c;然后放入两堆规模更小的石子&#xff08;新堆规模可以为 0 &#xff0c;且两个新堆的石子总数可以大于取走的那堆石子数&#xff09;&#xff0c;最后无法进行操作的人视为失…

如何在win系统部署Apache服务并实现无公网ip远程访问

文章目录 前言1.Apache服务安装配置1.1 进入官网下载安装包1.2 Apache服务配置 2.安装cpolar内网穿透2.1 注册cpolar账号2.2 下载cpolar客户端 3. 获取远程桌面公网地址3.1 登录cpolar web ui管理界面3.2 创建公网地址 4. 固定公网地址 前言 Apache作为全球使用较高的Web服务器…

MySQL解决 恢复从备份点到灾难点之间数据(不收藏找不到了)

CSDN 成就一亿技术人&#xff01; 今天分享一期 mysql中 备份之后发生灾难造成数据丢失 那么如何恢复中间的数据呢&#xff1f; 数据库数据高于一切&#xff08;任何数据是不能丢失的&#xff09; CSDN 成就一亿技术人&#xff01; 目录 1.准备测试数据库 2.备份数据库 观…