C#System.Text.RegularExpressions.Regex使用(二)

string x = "\\";
Regex r1 = new Regex("^\\\\$");
Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
Regex r2 = new Regex(@"^\\$");
Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
Regex r3 = new Regex("^\\$");
Console.WriteLine("r3 match count:" + r3.Matches(x).Count);//0
//匹配“\”

string x = "\"";
Regex r1 = new Regex("^\"$");
Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
Regex r2 = new Regex(@"^""$");
Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
//匹配双引号

7)组与非捕获组

string x = "Live for nothing,die for something";
string y = "Live for nothing,die for somebody";
Regex r = new Regex(@"^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$");
Console.WriteLine("x match count:" + r.Matches(x).Count);//1
Console.WriteLine("y match count:" + r.Matches(y).Count);//0
//正则表达式引擎会记忆“()”中匹配到的内容,作为一个“组”,并且可以通过索引的方式进行引用。
表达式中的“\1”,用于反向引用表达式中出现的第一个组,即粗体标识的第一个括号内容,“\2”则依此类推。

string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no([a-z]{5}),die for some\1$");
if (r.IsMatch(x))
{Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//输出:thing}
//获取组中的内容。注意,此处是Groups[1],因为Groups[0]是整个匹配的字符串,即整个变量x的内容。

string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no(?<g1>[a-z]{5}),die for some\1$");
if (r.IsMatch(x))
{Console.WriteLine("group1 value:" + r.Match(x).Groups["g1"].Value);
}//输出:thing
//
可根据组名进行索引。使用以下格式为标识一个组的名称(?<groupname>…)

string x = "Live for nothing nothing";
Regex r = new Regex(@"([a-z]+) \1");
if (r.IsMatch(x))
{x = r.Replace(x, "$1");
    Console.WriteLine("var x:" + x);//输出:Live for nothing}
//删除原字符串中重复出现的“nothing”。在表达式之外,使用“$1”来引用第一个组,下面则是通过组名来引用:
string x = "Live for nothing nothing";
Regex r = new Regex(@"(?<g1>[a-z]+) \1");
if (r.IsMatch(x))
{x = r.Replace(x, "${g1}");
    Console.WriteLine("var x:" + x);//输出:Live for nothing}

string x = "Live for nothing";
Regex r = new Regex(@"^Live for no(?:[a-z]{5})$");
if (r.IsMatch(x))
{Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//输出:(空)}
//在组前加上“?:”表示这是个“非捕获组”,即引擎将不保存该组的内容。

8)贪婪与非贪婪
正则表达式的引擎是贪婪,只要模式允许,它将匹配尽可能多的字符。通过在“重复描述字符”(*,+)后面添加“?”,可以将匹配模式改成非贪婪。

string x = "Live for nothing,die for something";
Regex r1 = new Regex(@".*thing");
if (r1.IsMatch(x))
{Console.WriteLine("match:" + r1.Match(x).Value);

//输出:Live for nothing,die for something}
Regex r2 = new Regex(@".*?thing");
if (r2.IsMatch(x))
{Console.WriteLine("match:" + r2.Match(x).Value); //输出:Live for nothing}

9)回溯与非回溯
使用“(?>…)”方式进行非回溯声明。
由于正则表达式引擎的贪婪特性,导致它在某些情况下,将进行回溯以获得匹配

string x = "Live for nothing,die for something";
Regex r1 = new Regex(@".*thing,");
if (r1.IsMatch(x))
{Console.WriteLine("match:" + r1.Match(x).Value); //输出:Live for nothing, }
Regex r2 = new Regex(@"(?>.*)thing,");
if (r2.IsMatch(x))//不匹配
{Console.WriteLine("match:" + r2.Match(x).Value); }
//在r1中,“.*”由于其贪婪特性,将一直匹配到字符串的最后,随后匹配“thing”,但在匹配“,”时
失败,此时引擎将回溯,并在“thing,”处匹配成功。在r2中,由于强制非回溯,所以整个表达式匹配失败。

10)正向预搜索、反向预搜索

①正向预搜索声明格式:正声明 “(?=…)”,负声明 “(?!...)” ,声明本身不作为最终匹配结果的一部分.

string x = "1024 used 2048 free";
Regex r1 = new Regex(@"\d{4}(?= used)");
if (r1.Matches(x).Count==1)
{Console.WriteLine("r1 match:" + r1.Match(x).Value);//输出:1024}
Regex r2 = new Regex(@"\d{4}(?! used)");
if (r2.Matches(x).Count==1)
{Console.WriteLine("r2 match:" + r2.Match(x).Value); //输出:2048}
//r1中的正声明表示必须保证在四位数字的后面必须紧跟着“ used”,r2中的负声明表示四位数字之后不能跟有“ used”。

②反向预搜索声明格式:正声明“(?<=)”,负声明“(?<!)”,声明本身不作为最终匹配结果的一部分.

string x = "used:1024 free:2048";
Regex r1 = new Regex(@"(?<=used:)\d{4}");
if (r1.Matches(x).Count==1)
{Console.WriteLine("r1 match:" + r1.Match(x).Value);//输出:1024}
Regex r2 = new Regex(@"(?<!used:)\d{4}");
if (r2.Matches(x).Count==1)
{Console.WriteLine("r2 match:" + r2.Match(x).Value);//输出:2048}
//r1中的反向正声明表示在4位数字之前必须紧跟着“used:”,r2中的反向负声明表示在4位数字之前必
须紧跟着除“used:”之外的字符串。

C#System.Text.RegularExpressions.Regex使用(二)

11)十六进制字符范围

正则表达式中,可以使用 "\xXX" "\uXXXX" 表示一个字符("X" 表示一个十六进制数)形式字符范围:
\xXX      
编号在 0255 范围的字符,比如:空格可以使用 "\x20" 表示。
\uXXXX  
任何字符可以使用 "\u" 再加上其编号的4位十六进制数表示,比如:汉字可以使用“[\u4e00-\u9fa5]”表示。

12)对[0,100]的比较完备的匹配需要特殊考虑的地方包括
*00
合法,00.合法,00.00合法,001.100合法
*
空字符串不合法,仅小数点不合法,大于100不合法
*
数值是可带后缀的,如“1.07f”表示该值为一个float类型(未考虑)
Regex r = new Regex(@"^\+?0*(?:100(\.0*)?|(\d{0,2}(?=\.\d)|\d{1,2}(?=($|\.$)))(\.\d*)?)$");
13)精确匹配有时候是困难的
有些需求要做到精确匹配比较困难,例如:日期、UrlEmail地址等,其中一些你甚至需要研究一些专门的文档写出精确完备的表达式,对于这种情况,只能退而求其次,保证比较精确的匹配。例如对于日期,可以基于应用系统的实际情况考虑一段较短的时间,或者对于像Email的匹配,可以只考虑最常见的形式。

转:http://blog.163.com/headmaster_01/blog/static/47556861200961035754508/

转:http://blog.163.com/headmaster_01/blog/static/47556861200961035754508/

6)特殊字符的匹配

原文地址:https://www.cnblogs.com/carl2380/p/1915701.html