how to match string exclude substring use re2

3.1k Views Asked by At

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?

2

There are 2 best solutions below

3
On BEST ANSWER

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

0
On

A re2-compatible solution:

^abc_(?:[a-zA-Z_:]{0,3}|[a-zA-Z_:]*(?:[a-zA-Z:][a-zA-Z_:]{3}|[a-zA-Z_:][b-zA-Z_:][a-zA-Z_:]{2}|[a-zA-Z_:]{2}[a-oq-zA-Z_:][a-zA-Z_:]|[a-zA-Z_:]{3}[a-oq-zA-Z_:]))_code$

It's not pretty, but I can't think of anything better with lookaround assertions missing from re2 (apart from not using re2 to solve this).

Here's a Go Playground link with a test: https://go.dev/play/p/rnXKLMIJyPk