如何匹配字符串排除子字符串使用re2

how to match string exclude substring use re2

使用 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 这可以匹配最后 5 个字符串。

如何只匹配前 5 个字符串?

前5个字符串和后5个字符串的区别在于,前5个字符串以_code结尾,后5个以_app_code结尾,其他所有内容在这种情况下保持不变.

要匹配所有前 5 个字符串,您可能需要使用 negative lookbehind

^abc_[a-zA-Z_]+(?<!_app)_code$
  • ^abc_表示字符串开始

  • _code$表示字符串结尾

  • 如果 _code 前面有 _app

  • (?<!_app) 告诉引擎不匹配

  • [a-zA-Z_]+ 表示此范围内介于

    之间的所有内容

您可以尝试使用此正则表达式 here