C#正则表达式整理备忘 5

来源:互联网 发布:微信淘宝客源码 编辑:程序博客网 时间:2024/06/05 09:35

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

Code
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
}

 

Code
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中,由于强制非回溯,所以整个表达式匹配失败。

 

Code
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”。

 

Code
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:”之外的字符串。

原创粉丝点击