views:

50

answers:

2

Hi!

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

+1  A: 
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();
Matthew Flaschen
Thanks, actually I made a mistake with the sought text. Not corrected.
Gregg Cleland
@Gregg, okay, I updated my solution.
Matthew Flaschen
Thanks Matthew. You = top guy.
Gregg Cleland
+1  A: 

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+]+?
Anon.
Aha! Thanks a lot for the nice clear reply. :-)
Gregg Cleland