Search code examples
re2

how to match string exclude substring use re2


use re2 https://github.com/google/re2/wiki/Syntax

abc_abc_code
abc_titer_code
abc_google_twitter_code
abc_twitter_twitter_code
abc_google_google_google_google_code

abc_abc_app_code
abc_titer_app_code
abc_google_twitter_app_code
abc_twitter_twitter_app_code
abc_google_google_google_google_app_code

abc_[a-zA-Z0-9_:]_app_code This can match last 5 string.

How to only match top 5 string?


Solution

  • The difference between to top 5 strings and the bottom 5 is that the top ones end with _code and the bottom ones end with _app_code, everything else, in this case, remain the same.

    To match all the top 5 strings then you might want to use a negative lookbehind

    ^abc_[a-zA-Z_]+(?<!_app)_code$
    
    • ^abc_ to indicate the string starting

    • _code$ to indicate the string ending

    • (?<!_app) tell the engine to not match if _code is preceded by _app

    • [a-zA-Z_]+ to indicate everything in this range that lies between

    You can experiment with this regex here