I'm trying to write a regular expression to match anything that isn't "foo" and "bar". I found how to match anything but one word at Regular expression to match a line that doesn't contain a word? but I'm not very skilled with regex and am unsure of how to add a second word to this critera.
我正在尝试编写一个正则表达式来匹配任何不是“foo”和“bar”的表达式。我发现如何在正则表达式中匹配除了一个词之外的任何东西,以匹配不包含一个词的行?但是我对regex不是很熟练,我不知道如何在这个critera中添加第二个单词。
Any help would be most appreciated!
非常感谢您的帮助!
CLARIFICATION:
澄清:
I wanted to match on anything that wasn't EXACTLY foo or bar.
我想要匹配任何不是foo或bar的东西。
3 个解决方案
#1
63
Answer to the question: "A Regular Expression to match anything that isn't "foo" and "bar"?"
回答问题:“一个正则表达式匹配任何非“foo”和“bar”的表达式?”
^(?!foo$|bar$).*
would do exactly that.
会做。
^ # Start of string
(?! # Assert that it's impossible to match...
foo # foo, followed by
$ # end of string
| #
bar$ # bar, followed by end of string.
) # End of negative lookahead assertion
.* # Now match anything
You might need to set RegexOptions.Singleline
if your string can contain newlines that you also wish to match.
您可能需要设置RegexOptions。单线,如果您的字符串可以包含您也希望匹配的新行。
#2
17
Answer to the question: "How to add a second word to this critera?"
回答问题:“如何在这个critera中增加第二个单词?”
The answer of the question you link to is:
你链接到的问题的答案是:
^((?!word).)*$
Where (?!word)
is a negative look-ahead. The answer for this question is:
哪里(?!)是一个消极的展望。这个问题的答案是:
^((?!wordone|wordtwo).)*$
Works for both words. Note: you should enable the global and multiline options, if you have multiple lines and want to match for each line, as the other question.
这两个单词。注意:您应该启用全局和多行选项,如果您有多个行,并且希望匹配每一行,这是另一个问题。
The difference is the negative look-ahead clause: (?!wordone|wordtwo)
. It can be extended to any (reasonable) amount of words or clauses.
不同之处在于消极的前瞻子句:(?!wordone|wordtwo)。它可以扩展到任何(合理的)字数或从句。
See this answer for a detailed explanation.
有关详细解释,请参见此答案。
#3
6
I get what you want to do, but the specifics of what you want to block/allow are a little unclear. For example, do you want to block anything that isn't exactly foo
or bar
? Or do you want to block anything containing those two strings?
我知道你想要做什么,但是你想要阻止/允许的细节有点不清楚。例如,您想要阻止任何不完全是foo或bar的东西吗?或者你想要阻止任何包含这两个字符串的东西吗?
Can they be part of another string, as in @Tim's foonly
or bartender
examples?
它们可以是另一个字符串的一部分吗,就像@Tim的foonly或调酒师的例子一样?
I'm just going to suggest patterns for each one:
我将为每一个人建议模式:
/^(?!foo$|bar$).*/ #credit to @Tim Pietzcker for this one, he did it first
#blocks "foo"
#blocks "bar"
#allows "hello goodbye"
#allows "hello foo goodbye"
#allows "foogle"
#allows "foobar"
/^(?!.*foo|.*bar).*$/
#blocks "foo"
#blocks "bar"
#allows "hello goodbye"
#blocks "hello foo goodbye"
#blocks "foogle"
#blocks "foobar"
/^(?!.*\b(foo|bar)\b).*$/
#blocks "foo"
#blocks "bar"
#allows "hello goodbye"
#blocks "hello foo goodbye"
#allows "foogle"
#allows "foobar"
#1
63
Answer to the question: "A Regular Expression to match anything that isn't "foo" and "bar"?"
回答问题:“一个正则表达式匹配任何非“foo”和“bar”的表达式?”
^(?!foo$|bar$).*
would do exactly that.
会做。
^ # Start of string
(?! # Assert that it's impossible to match...
foo # foo, followed by
$ # end of string
| #
bar$ # bar, followed by end of string.
) # End of negative lookahead assertion
.* # Now match anything
You might need to set RegexOptions.Singleline
if your string can contain newlines that you also wish to match.
您可能需要设置RegexOptions。单线,如果您的字符串可以包含您也希望匹配的新行。
#2
17
Answer to the question: "How to add a second word to this critera?"
回答问题:“如何在这个critera中增加第二个单词?”
The answer of the question you link to is:
你链接到的问题的答案是:
^((?!word).)*$
Where (?!word)
is a negative look-ahead. The answer for this question is:
哪里(?!)是一个消极的展望。这个问题的答案是:
^((?!wordone|wordtwo).)*$
Works for both words. Note: you should enable the global and multiline options, if you have multiple lines and want to match for each line, as the other question.
这两个单词。注意:您应该启用全局和多行选项,如果您有多个行,并且希望匹配每一行,这是另一个问题。
The difference is the negative look-ahead clause: (?!wordone|wordtwo)
. It can be extended to any (reasonable) amount of words or clauses.
不同之处在于消极的前瞻子句:(?!wordone|wordtwo)。它可以扩展到任何(合理的)字数或从句。
See this answer for a detailed explanation.
有关详细解释,请参见此答案。
#3
6
I get what you want to do, but the specifics of what you want to block/allow are a little unclear. For example, do you want to block anything that isn't exactly foo
or bar
? Or do you want to block anything containing those two strings?
我知道你想要做什么,但是你想要阻止/允许的细节有点不清楚。例如,您想要阻止任何不完全是foo或bar的东西吗?或者你想要阻止任何包含这两个字符串的东西吗?
Can they be part of another string, as in @Tim's foonly
or bartender
examples?
它们可以是另一个字符串的一部分吗,就像@Tim的foonly或调酒师的例子一样?
I'm just going to suggest patterns for each one:
我将为每一个人建议模式:
/^(?!foo$|bar$).*/ #credit to @Tim Pietzcker for this one, he did it first
#blocks "foo"
#blocks "bar"
#allows "hello goodbye"
#allows "hello foo goodbye"
#allows "foogle"
#allows "foobar"
/^(?!.*foo|.*bar).*$/
#blocks "foo"
#blocks "bar"
#allows "hello goodbye"
#blocks "hello foo goodbye"
#blocks "foogle"
#blocks "foobar"
/^(?!.*\b(foo|bar)\b).*$/
#blocks "foo"
#blocks "bar"
#allows "hello goodbye"
#blocks "hello foo goodbye"
#allows "foogle"
#allows "foobar"