views:

145

answers:

3
Source:    <prefix><content1><suffix1><prefix><content2><suffix2>
Engine:    PCRE

RegEx1:    (?<=<prefix>)(.*)(?=<suffix1>)
RegEx2:    (?<=<prefix>)(.*)(?=<suffix2>)

Result1:   <content1>
Result2:   <content1><suffix1><prefix><content2>

The desired result for RegEx2 is just <content2> but it is obviously greedy. How do I make RegEx2 non-greedy and use only the last matching lookbehind?

[I hope I have translated this correctly from the NoteTab syntax. I don't do much RegEx coding. The <prefix>, <content> & <suffix> terms are just meant to represent arbitrary strings. Only the "<" in the "?<=" lookbehind command is significant.]

I suspect it is something simple but after too many hours of searching I'm giving up on solving it myself.

Thanks for the help

Art

A: 

Put something greedy in front of it?

(?:.*)(?<=<prefix>)(.*)(?=<suffix2>)

Since the greedy (?:.*) will gobble as much as it can, only the minimum will be matched by the rest of the pattern - effectively making the rest non-greedy.

The non-greedy .*? might also work:

(?<=<prefix>)(.*?)(?=<suffix2>)
Amber
THe first regex looks OK (of course you'll then need to use \1 and not the entire match), the second won't work. It will still match too much (because the first possible match wins, and the long match is still possible).
Tim Pietzcker
Yeah, I wasn't sure on the second, hence the "might".
Amber
Thanks Dav. I'll give it a try but I was hoping that RegEx had something more elegant such as a relative identifier of some sort similar to the subpattern numbers.
+1  A: 

I suggest you use:

(?<=<prefix>)(((?!<prefix>).)*)(?=<suffix2>)

This makes sure that there can be no <prefix> inside the match. The complete match result will be <content2>

Tim Pietzcker
A: 

I just had the same problem. But in my case it was

(?<=<prefix>)(?:.(?!<prefix>))*(?=<suffix>)

that did what I wanted.

This expression will match anything that is a concatenation of characters between <prefix> and <suffix> and doesn't contain the substring <prefix>. (I think so. I'm not very good at regexp.)

5gon12eder