Search code examples
c#.netregexregex-lookaroundslookbehind

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. 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)";


Solution

  • 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+]+?