.NET 中的惰性正则表达式匹配。这里出了什么问题?
在下面的示例中,我想检索 pMAINp 和 first pMDSp 之间的文本。正则表达式有一个向后看和一个向前看:
string contents = "pMAINp MAP B FlightTest Load pMDSp ZutiCarrier pWingp some pMDSp more pWingp end";
string blockMainRegex = @"(?<=pMAINp)[\s\w+]+(?=(pMDS)?)";
我希望的结果是: “ MAP B FlightTest Load ”
但它返回的是: “MAP B FlightTest Load pMDSp ZutiCarrier pWingp some pMDSp more pWingp end”
您会注意到我正在尝试在这里进行惰性匹配:(pMDS)?这显然不起作用! 任何对此的帮助将不胜感激。谢谢。 :-)
编辑:哎呀,所寻求的文本已被更正。
这很好用:
字符串 blockMainRegex = @"(?<=pMAINp)[\s\w+]+?(?=pMDS)";
In the following example I would like to retrieve the text between pMAINp and the first pMDSp. The regex has a look-behind and a look-ahead:
string contents = "pMAINp MAP B FlightTest Load pMDSp ZutiCarrier pWingp some pMDSp more pWingp end";
string blockMainRegex = @"(?<=pMAINp)[\s\w+]+(?=(pMDS)?)";
The result I was hoping for was:
" MAP B FlightTest Load "
but what it returns is:
"MAP B FlightTest Load pMDSp ZutiCarrier pWingp some pMDSp more pWingp end"
You'll notice that I'm attempting a lazy match here: (pMDS)? which clearly isn't working!
Any help with this would be much appreciated. Thanks. :-)
EDIT: Whoops, the sought text has been corrected.
This works great:
string blockMainRegex = @"(?<=pMAINp)[\s\w+]+?(?=pMDS)";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎误解了惰性匹配的工作原理。
您将惰性运算符应用于量词 - *、+、?等等 - 在其他地方,它被解释为“零或一”。
如果您希望正则表达式的一部分匹配尽可能少的字符,请将惰性运算符应用于与正则表达式的该部分关联的量词 - 在这种情况下,您希望像这样使用它:
You seem to be misunderstanding how lazy-matching works.
You apply the lazy operator to a quantifier - *, +, ? etc. - anywhere else, it's interpreted as "zero-or-one".
If you want one part of the regex to match as few characters as possible, apply the lazy operator to the quantifier associated with that part of the regex - in this case, you want to use it like so:
第一组会有你想要的东西。例如:
The first group will have what you want. E.g.: