开发者

Lazy Regex Match in .NET. What's wrong here?

开发者 https://www.devze.com 2023-01-04 18:22 出处:网络
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:

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 开发者_StackOverflow社区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)";


You'll notice that I'm attempting a lazy match here: (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. E.g.:

Regex re = new Regex(@"pMAINp(.*?)pMDSp");
string result = re.Match(contents).Groups[1].ToString();
0

精彩评论

暂无评论...
验证码 换一张
取 消