Disclaimer: This only works in select regex engines.
Some regex engines have a "feature" that we can abuse: Capture groups in lookaheads are possessive; once they have matched they can never change their value again.
Making use of this "feature", the regex can be written like this:
.*?(?!\1)thing_you_want_the_first_occurrence_of(?=())rest_of_the_regex
In this specific case, that looks like this (the indices of the capture groups are shifted by 1 since foo=([^,]*)
contains a capture group):
.*?(?!\2)(?<![^,])foo=([^,]*),(?=())bar=2(?![^,])
So, how does it work?
After the first occurrence of foo=
is found, the group (?=())
matches. Because it's inside a lookahead, it can never change its value anymore - not even backtracking can affect it. So from this point onwards, (?!\2)
will never match again. The fact that the first occurrence of foo=
has been found is now "locked in" and cannot be undone. If the regex backtracks and tries to make the .*?
match more text, the (?!\2)
prevents this.
Demo using python's PyPI regex
module:
>>> pattern = r'.*?(?!\2)(?<![^,])foo=([^,]*),(?=())bar=2(?![^,])'
>>> regex.match(pattern, 'baz=0,foo=1,bar=2,foo=3,bar=4').group(1)
'1'
>>> regex.match(pattern, 'baz=0,foo=1,foo=1,bar=2')
>>>