c# 正则表达式

 

 

正则很强大,每次用每次查。故整理一下,基于Unity3D平台,做一些常用的操作测试。

正则表达式是一种匹配输入文本的模式。.Net 框架提供了允许这种匹配的正则表达式引擎。模式由一个或多个字符、运算符和结构组成。

 

 

常用如下:

 





直接上代码:

 

usingSystem.Collections;

usingSystem.Collections.Generic;

usingUnityEngine;

usingSystem.Text.RegularExpressions;

 

publicclassNewBehaviourScript : MonoBehaviour

{

    voidStart()

{

TestIsMatch();

        TestMatch();

        TestSplit();

        TestGroup();

        TestDelegate();

        TestReplace();

}

 

  ///<summary>

    /// IsMatch

    ///</summary>

    privatevoidTestIsMatch()

    {

        boola = Regex.IsMatch("3234", @"\d");

        Debug.Log(a); //true

        boolb = Regex.IsMatch("fdasg", @"\d");

        Debug.Log(b); //false

    }




 

    ///<summary>

    /// Match

    ///</summary>

    privatevoidTestMatch()

    {

        stringstr1 = "9s%!@av3g#$|+90=~72";

        stringpattern1 = @"\d|[a-z]";                //十进制数字匹配 a-z之前的的单个字符

        MatchCollectioncol = Regex.Matches(str1, pattern1);

        foreach (Matchmatchincol)                  //遍历了所有符合规则的

        {

            Debug.Log(match.ToString());

        }

}

 



    ///<summary>

    /// Split

    ///</summary>

    privatevoidTestSplit()

    {

        stringstr2 = "aa;bb,cc.dd[ee.ff";

        stringpattern2 = @"[;,.]";

        string[] strArray = Regex.Split(str2, pattern2); //多个拆分

        for (inti = 0; i < strArray.Length; i++)

        {

            Debug.Log(strArray[i]);

        }

    }




    ///<summary>

    /// Group 分组

    ///</summary>

    privatevoidTestGroup()

    {

        //分组

        stringline = "lane=1;speed=30.3mph;acceleration=2.5mph/s";

        Regexreg = newRegex(@"speed\s*=\s*([\d\.]+)\s*(mph|km/h|m/s)*");

        Matchmatchs = reg.Match(line);

        Debug.Log(matchs.Groups[0].Value);         //speed=30.3mph

        Debug.Log(matchs.Groups[1].Value);         //30.3

        Debug.Log(matchs.Groups[2].Value);         //mph

 

        //分组命名访问  利用 (?<xxx>子表达式) 定义分组别名,这样就可以利用 Groups["xxx"] 进行访问分组/子表达式内容。

        RegexregCreatKey = newRegex(@"speed\s*=\s*(?<name1>[\d\.]+)\s*(?<name2>mph|km/h|m/s)*");

        MatchmatchsKey = regCreatKey.Match(line);

        Debug.Log(matchsKey.Groups[0].Value);     //speed=30.3mph

        Debug.Log(matchsKey.Groups[1].Value);     //30.3

        Debug.Log(matchsKey.Groups[2].Value);     //mph

        Debug.Log(matchsKey.Groups["name1"].Value);//30.3

        Debug.Log(matchsKey.Groups["name2"].Value);//mph

    }

 



    ///<summary>

    ///使用MatchEvaluator委托

    ///</summary>

    privatevoidTestDelegate()

    {

        varsource = "123abc456ABCD789";

        Regexregex = newRegex("[A-Z]{3}", RegexOptions.IgnoreCase);    //忽略大小写

        varnewSource = regex.Replace(source, newMatchEvaluator(OutPutMatch));

        Debug.Log("原字符串:" + source);                                //原字符串:123abc456ABCD789

        Debug.Log("替换后的字符串:" + newSource);                       //替换后的字符串:123 - abc - 456 - ABC - D789

 

    }

    privatestringOutPutMatch(Matchmatch)

    {

        return"-" + match.Value + "-";

}

 



    ///<summary>

    ///匹配替换

    ///</summary>

    voidTestReplace()

    {

        stringstr = "aasb说什么啊";

        stringpattern1 = @"a|b|什么阿";                  //全字匹配 --res1 :**s*说什么啊

        stringpattern2 = @"[ab什么]";                    //只要有就匹配 --res2 :**s***

        stringpattern3 = "^";

        stringres1 = Regex.Replace(str, pattern1, "*");

        Debug.Log("res1:" + res1);                       //res1 :**s*说什么啊

 

        stringres2 = Regex.Replace(str, pattern2, "*");

        Debug.Log("res2:" + res2);                       //res2 :**s***

 

        stringres3 = Regex.Replace(str, pattern3, "开始:");

        Debug.Log("res3:" + res3);                       //res3 :开始:aasb说什么啊

 

        stringzb = "(120,30)";

        stringr = Regex.Replace(zb, @"\(|\)", "");

        string[] array = r.Split(',');

        intx = int.Parse(array[0]);

        inty = int.Parse(array[1]);

        Debug.Log("x:" + x + " ___ y :" + y);           //x :120 ___ y :30

 

 

        MatchCollectioncol = Regex.Matches(zb, @"\d*,\d*");

        foreach (varitemincol)

        {

            Debug.Log("__" +item.ToString());            //__120,30

        }

    }

}

 

  • 6
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿成_

你的鼓励是我创作的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值