c#正则表达式——RegexOptions

本文详细介绍了RegexOptions枚举在正则表达式中的作用,包括不区分大小写、从右往左匹配、单行模式和多行模式的设置,以及如何使用Split方法分割字符串。通过实例展示了不同选项对匹配结果的影响,帮助理解正则表达式的高级用法。
摘要由CSDN通过智能技术生成

1、RegexOptions.None
不设置任何选项

string pattern = "abc_";
        Regex regex = new Regex(pattern, RegexOptions.None);
        string inputString = "agcABc_,abc_,Abc_,abC_\naBc_,ABc_";
        Console.WriteLine(regex.Match(inputString).Value);

输出:

abc_

2、 RegexOptions.IgnoreCase

指定匹配时不区分大小写

 string pattern = "abc_";
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
            string inputString = "agcABc_,abc_,Abc_,abC_\naBc_,ABc_";
            MatchCollection matchCollection1 = regex.Matches (inputString);
            foreach (Match item in matchCollection1)
            {
                Console.WriteLine(item.Value );
            }

输出:

ABc_
abc_
Abc_
abC_
aBc_
ABc_

3、RegexOptions.RightToLeft
指定匹配时从右往左开始匹配

   string pattern = "abc_";
            Regex regex = new Regex(pattern, RegexOptions.RightToLeft | RegexOptions.IgnoreCase);
            string inputString = "agcABc_,abc_,Abc_,abC_\naBc_,ABc_";
            MatchCollection matchCollection1 = regex.Matches (inputString);
            foreach (Match item in matchCollection1)
            {
                Console.WriteLine(item.Value );
            }

输出:

ABc_
aBc_
abC_
Abc_
abc_
ABc_

4、RegexOptions.Singleline
该选项的作用就是改变(.)的功能,默认情况下(.)不能匹配\n,就像下面代码一样,碰到了\n匹配就断了,但是如果指定了RegexOptions.Singleline,那么(.)就可以匹配\n了,此时(.)可以匹配所有字符。

  string pattern = "^.+";
            string input = "This is one line and" +"\n" + "this is the second.";
            foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Singleline))
                Console.WriteLine(Regex.Escape(match.Value));

            Console.WriteLine();
            foreach (Match match in Regex.Matches(input, pattern))
                Console.WriteLine(Regex.Escape(match.Value));

输出:

This\ is\ one\ line\ and\nthis\ is\ the\ second\.

This\ is\ one\ line\ and

5、 RegexOptions.Multiline
更改 ^ 和 $ 的含义,使它们分别在任意一行的行首和行尾匹配,而不仅仅在整个字符串的开头和结尾匹配,就像下面的代码一样没有使用RegexOptions.Multiline之前,^ 只能匹配整个字符串的开头,但是使用了RegexOptions.Multiline以后,只要字符串中有\n,那么^就可以在新的一行进行匹配行的开头,$可以在新的一行匹配行的结尾。

 string pattern = "^This";
            string input = "This is one line and" +"\n" + "This is the second.";
           

           
            foreach (Match match in Regex.Matches(input, pattern))
                Console.WriteLine(Regex.Escape(match.Value));

            Console.WriteLine();
            foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline))
                Console.WriteLine(Regex.Escape(match.Value));



             pattern = "and$";
             input = "This is one line and" + "\n" + "This is the second line and";


            Console.WriteLine();
            foreach (Match match in Regex.Matches(input, pattern))
                Console.WriteLine(Regex.Escape(match.Value));

            Console.WriteLine();
            foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline))
                Console.WriteLine(Regex.Escape(match.Value));
This

This
This

and

and
and

6、Split
按照模式去分割字符串

 string pattern = "This";
            string input = "beginThis is one line and This is second line and This is third line";

            Regex regex = new Regex(pattern);
            string[] stringArray = regex.Split(input).SkipWhile (item=>item=="").ToArray ();//去掉""项
            stringArray.ToList().ForEach(item => Console.WriteLine(item));
            Console.WriteLine();

             stringArray = regex.Split(input,2).SkipWhile(item => item == "").ToArray();//指定分割出来的字符数组大小
            stringArray.ToList().ForEach(item => Console.WriteLine(item));
            Console.WriteLine();

            stringArray = regex.Split(input,2,8).SkipWhile(item => item == "").ToArray();//指定分割出来的字符数组大小而且指定从哪个位置开始进行匹配分割
            stringArray.ToList().ForEach(item => Console.WriteLine(item));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

c#上位机

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值