.NET正则表达式查找不贪婪
如何让后面的人变得贪婪?.NET正则表达式查找不贪婪,.net,regex,lookbehind,.net,Regex,Lookbehind,如何让后面的人变得贪婪? 在本例中,我希望lookback使用:if存在 m = Regex.Match("From: John", @"(?i)(?<=from:)...."); // returns ' Jon' what I expect not a problem just an example m = Regex.Match("From: John", @"(?i)(?<=from:?)...."); // returns ': Jo' // I want it to r
在本例中,我希望lookback使用:if存在
m = Regex.Match("From: John", @"(?i)(?<=from:)....");
// returns ' Jon' what I expect not a problem just an example
m = Regex.Match("From: John", @"(?i)(?<=from:?)....");
// returns ': Jo'
// I want it to return ' Jon'
m=Regex.Match(“From:John,@”(?)i)(?有趣的是,我没有意识到他们在.NET中是不贪婪的。这里有一个解决方案:
(?<=from(:|(?!:)))
这迫使它匹配“:”,如果存在的话,
很少冗长,但是它有效,我认为它是一个错误,它的后面是不贪婪的。@ BLAM:只是意识到这可以简化为不冗赘。我更新了我的帖子。没有失败的编译器,请修复或返回原来的答案rBase=新正则表达式(@)(?i)(?@Blam:你的示例中有一个输入错误-最后一个“)”不应该在那里。新的正则表达式(@)(?i)(?谢谢,这似乎有效。我已经给了你+1,所以我不能再做了。正则表达式很酷,但不是很宽容。
(?<=from(:|(?!:)))
(
: # match a ':'
|
(?!:) # otherwise match nothing (only if the next character isn't a ':')
)