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));