栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

c#正则表达式——RegexOptions

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c#正则表达式——RegexOptions

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 andnthis 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));
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910763.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号