英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论