模式匹配和文本处理

模式匹配和文本处理
字符串的处理主要包括寻找字符中的模式(模式匹配),以及通过称为正则表达式的特殊语言来执行
正则表达式
正则表达式是一种用于描述字符串格式的语言,它提供了对应于重复字符、替换字符以及分组字符的描述符。其既可进行字符串的搜索,也可以用于字符串的替换。它也是一种字符串。
正则表达式中包含的元字符可以进行大范围的搜索等操作。
C#使用正则表达式
首先需引入RegEx类,存在于System.Text.RegularExpression名字域中。
匹配:Match类(只能存储一个匹配)
匹配s:Matches类(存储多个匹配),然后可以把找到的匹配存储到MatchCollection对象中。
实例:
  static void Main( string [] args)
        {
#if false
            Regex reg = new Regex("the");//创建一个RegEx对象,并把要匹配的正则表达式传递给构造器。
            string str1 = "The quick brown fox jumped over the lazy dog";
            Match matchset;//match类为存储用来与正则表达式进行匹配的数据提供方法。
            int matchpos;
            matchset = reg.Match(str1);
            string matchStr = matchset.Value;
            Console.WriteLine(matchStr);
            if (matchset.Success)
            {
                matchpos = matchset.Index;
                Console.WriteLine("found match at position:" + matchpos);
            }
            //if (Regex.IsMatch(str1, "the"))
            //{
            //    Match aMatch;
            //    aMatch = reg.Match(str1);
            //}
#endif
            string str2 = "the quick brown fox jumped over the lazy dog" ;
            Console .WriteLine(str2);
            Console .Write( "你想找啥?" );
            string regEx = Console .ReadLine();
            Regex reg = new Regex (regEx);
            MatchCollection matchSet;
            matchSet = reg.Matches(str2);
            if (matchSet.Count > 0)
            {
                foreach ( Match aMatch in matchSet)
                {
                    Console .WriteLine( "found a match at:" + aMatch.Index);
                }
            }
        }
替换:Replace类
参数:目标字符串、要替换的子串、用作替换的子串。
示例代码:
            str2 = Regex .Replace(str2, "brown" , "black" );
            Console .WriteLine(str2);
用数量词精确匹配次数
加号(+):说明正则表达式应该匹配一个或多个紧接其前的字符。
星号(*):说明正则表达式应该匹配零个或多个紧接其前的字符。但是这个数量词较为难用,常会匹配出全部字符串,起不到作用。

问号(?):精确匹配零次或一次的数量词。代表可选项。把它加在一个字符的后面,就表示此处容许出现这个字符,不过它的出现并非匹配成功的必要条件。
例如:“color colour”使用正则表达式“colou?r”进行匹配,可以匹配到color和colour;
         “bad baaad”使用正则表达式“baa?d”匹配,可以匹配到bad,而匹配不到baaad,因为?之前紧接的字符“a”可以匹配也可以不匹配,也就是可以匹配到bad和baad;
           “bad baaaad”使用“baaa?d”只能匹配到baad和baaad,依次结果一个也匹配不到。
{n}:指定此符号之前紧接的字符出现的次数。
代码:
//使用{}
            string [] words4 = new string [] { "baaad" , "baad" , "boy" , "bad" , "bear" , "bend" };
            Console .WriteLine( "使用“{}”的匹配结果" );
            foreach ( string word in words4)        
            {
                if ( Regex .IsMatch(word, "ba{2}d" )) //
                {
                    Console .WriteLine(word);
                }
            }
结果:

{n,m}:指定匹配的最大值(m)和最小值(n)。
//使用{n,m}
            string [] words5 = new string [] { "baaad" , "baad" , "boy" , "bad" , "bear" , "bend" };
            Console .WriteLine( "使用“{n,m}”的匹配结果" );
            foreach ( string word in words4)
            {
                if ( Regex .IsMatch(word, "ba{1,3}d" )) //
                {
                    Console .WriteLine(word);
                }
            }
结果:

{n,}:指定最少匹配的数量。
代码:
//使用{n,}
            string [] words6 = new string [] { "baaad" , "baad" , "boy" , "bad" , "bear" , "bend" };
            Console .WriteLine( "使用“{n,m}”的匹配结果" );
            foreach ( string word in words6)
            {
                if ( Regex .IsMatch(word, "ba{2,}d" )) //
                {
                    Console .WriteLine(word);
                }
            }
结果:

以上则是所谓的 贪心行为,即是尽可能多的匹配,但会导致不可预期的匹配。
字符类 使用主要元素来构成正则表达式
句点(.):句点与字符串中的每一个单独字符匹配。
代码:
            string str1 = "The quick brown fox jumped over the lazy dog." ;
            MatchCollection matchSet;
            matchSet = Regex .Matches(str1, "." );
            foreach ( Match match in matchSet)
            {
                Console .WriteLine( "Matches at: " + match.Index);
            }

较好地利用句点的方法:用它在字符串内部定义字符范围,也就是用来限制字符串的开始(或/和)结束字符。
代码:
            string str1 = "The quick brown fox jumped over the lazy dog." ;
            MatchCollection matchSet;
            matchSet = Regex .Matches(str1, "j....d" );
            foreach ( Match match in matchSet)
            {
                Console .WriteLine( "Matches at: " + match.Index);
            }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值