[英]Lazy Regex Match in .NET. What's wrong here?
In the following example I would like to retrieve the text between pMAINp and the first pMDSp. 在以下示例中,我想检索pMAINp和第一个 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 " 我希望的结果是:“MAP B FlightTest Load”
but what it returns is: "MAP B FlightTest Load pMDSp ZutiCarrier pWingp some pMDSp more pWingp end" 但它返回的是:“MAP B FlightTest加载pMDSp ZutiCarrier pWingp一些pMDSp更多pWingp结束”
You'll notice that I'm attempting a lazy match here: (pMDS)? 你会注意到我在这里尝试一个懒惰的比赛:(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)"; string blockMainRegex = @“(?<= pMAINp)[\\ s \\ w +] +?(?= pMDS)”;
You'll notice that I'm attempting a lazy match here: (pMDS)?
你会注意到我在这里尝试一个懒惰的比赛:(pMDS)? which clearly isn't working!
这显然不起作用!
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: 如果您希望正则表达式的一部分匹配尽可能少的字符,请将延迟运算符应用于与正则表达式的该部分关联的量词 - 在这种情况下,您希望像这样使用它:
[\s\w+]+?
string blockMainRegex = @"pMAINp(.*?)pMDSp";
The first group will have what you want. 第一组将拥有你想要的东西。 Eg:
例如:
Regex re = new Regex(@"pMAINp(.*?)pMDSp");
string result = re.Match(contents).Groups[1].ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.