What is the regular expression for matching everything except two specific strings?

huangapple go评论45阅读模式
英文:

What is the regular expression for matching everything except two specific strings?

问题

I want a regular expression to match everything but two strings, STRING1 and STRING2.

This is what I tried:

^(?!STRING1|STRING2).*$ is close, but it does not match STRING1X, which should be matched, because STRING1X is not equal to STRING1 and not equal to STRING2.

^(?!STRING1|STRING2)$ doesn't match anything.

英文:

I want a regular expression to match everything but two strings, STRING1 and STRING2.

This is what I tried:

^(?!STRING1|STRING2).*$ is close, but it does not match STRING1X, which should be matched, because STRING1X is not equal to STRING1 and not equal to STRING2.

^(?!STRING1|STRING2)$ doesn't match anything.

答案1

得分: 0

^(?!STRING1$|STRING2$).*

解释:

^ 表示字符串的开头。

(?!STRING1$|STRING2$) 是一个负向前瞻断言,排除了精确匹配字符串 "STRING1" 和 "STRING2"。$ 锚点确保匹配包括整个字符串。

.* 匹配任意字符(除换行符外)零次或多次。

英文:
^(?!STRING1$|STRING2$).*

Explanation:

^ asserts the start of the string.

(?!STRING1$|STRING2$) is a negative lookahead assertion that excludes matches for the exact strings "STRING1" and "STRING2". The $ anchor ensures that the match includes the entire string.

.* matches any character (except newline) zero or more times.

huangapple
  • 本文由 发表于 2023年6月29日 20:30:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定