Search code examples
regexregex-negationregex-lookaroundsregex-groupregex-greedy

regex to match one string and negate the other


I need a regex to match Test< string only from Test<OKAY> and it should not match Test<?>

Test<?>  // shouldn't match
Test<OKAY> // should match

I tried Test<[^?] but it matches Test<O instead of Test< from Test<OKAY>

How do I fix this?


Solution

  • After Test<, you need positive lookahead for non-question-mark characters, followed by > to indicate the end of the brackets:

    Test<(?=[^?]+>)
    

    https://regex101.com/r/gxpq3E/1