0

String to be matched:

id=abc somethingelse value=1 value=2 value=3 value=1

String not to be matched:

id=abc somethingelse value=2 value=2 value=3 value=1

I would like to:

  • skip the somethingelse string, i.e., can't predict what this string is, so must use wildcard
  • match the first occurrence value=1
  • if the first value does not equal to 1, ignore any other occurrences and abort. So the second example should not be matched.

I tried id=abc.*?value=1 in regex101, but obviously it doesn't stop at the first value...

Many thanks.

DBSQUARED
  • 71
  • 5
  • So for the first string, you want two separate matches of `id=abc` and `value=1` ("skip the somethingelse"?), and for the second, you want no matches at all, is that right? – CertainPerformance Mar 07 '19 at 08:40

1 Answers1

0

You may use this regex with negative lookahead:

^id=abc(?:(?!value=).)*value=1

RegEx Demo

Key part is use of (?:(?!value=).)* which means match any character as long as it is not followed by value= (enforced by negative lookahead (?!value=)).

anubhava
  • 664,788
  • 59
  • 469
  • 547