Two greedy quantifiers in the same regex

  • A+
Category:Languages

If I have an unknown string of the structure:

"stuff I don't care about THING different stuff I don't care about THING ... THING even more stuff I don't care about THING stuff I care about"

I want to capture the "stuff I care about" which will always be after the last occurrence of THING. There is the potential for 0 occurrences of THING, or many. If there are 0 occurrences then there is no stuff I care about. The string can't start or end with THING.

Some possible strings:

"stuff I don't care about THING stuff I care about"

"stuff I don't care about"

Some not possible strings:

"THING stuff I care about"

"stuff I don't care about THING stuff I don't care about THING"


My current solution to this problem is to use a regex with two greedy quantifiers as follows:

if( /.*THING(.*)/ ) {     $myStuff = $1; } 

It seems to be working, but my question is about how the two greedy quantifiers will interact with each other. Is the first (leftmost) greedy quantifier always "more greedy" than the second?

Basically am I guaranteed not to get a split like the following:

"stuff I don't care about THING"

$1 = "different stuff I don't care about THING even more stuff I don't care about THING stuff I care about"

Compared to the split I do want:

"stuff I don't care about THING different stuff I don't care about THING even more stuff I don't care about THING"

"stuff I care about"

 


Regex returns the longest leftmost match. The first wildcard will initially match through to the end of line, then successively backtrack a character at a time until the rest of the regex yields a match, i.e. so that the last THING in the string is matched.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: