Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 正则表达式匹配除两个单词以外的任何内容_.net_Regex_Regex Negation - Fatal编程技术网

.net 正则表达式匹配除两个单词以外的任何内容

.net 正则表达式匹配除两个单词以外的任何内容,.net,regex,regex-negation,.net,Regex,Regex Negation,我试图编写一个正则表达式来匹配任何不是“foo”和“bar”的东西。我发现除了一个单词外,我什么都可以匹配,但我对正则表达式不是很熟练,我不确定如何在这个标准中添加第二个单词 任何帮助都将不胜感激 澄清: 我想匹配任何不完全是foo或bar的东西。回答问题:“一个正则表达式来匹配任何不是foo和bar的东西”?“ 我会这么做的 ^ # Start of string (?! # Assert that it's impossible to match... foo # fo

我试图编写一个正则表达式来匹配任何不是“foo”和“bar”的东西。我发现除了一个单词外,我什么都可以匹配,但我对正则表达式不是很熟练,我不确定如何在这个标准中添加第二个单词

任何帮助都将不胜感激

澄清:


我想匹配任何不完全是foo或bar的东西。

回答问题:“一个正则表达式来匹配任何不是foo和bar的东西”?“

我会这么做的

^      # 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

如果您的字符串可以包含您也希望匹配的换行符,您可能需要设置
RegexOptions.Singleline

回答问题:“如何向中添加第二个单词?”

您链接到的问题的答案是:

^((?!word).)*$
其中,
(?!word)
是一种消极的前瞻。这个问题的答案是:

^((?!wordone|wordtwo).)*$
这两个词都适用。注意:如果您有多行并且希望与每行匹配,则应启用全局和多行选项,如另一个问题所示

不同之处在于消极前瞻子句:
(?!wordone | wordtwo)
。它可以扩展到任何(合理)数量的单词或子句


有关详细说明,请参阅。

我知道您想要做什么,但您想要阻止/允许什么的具体内容有点不清楚。例如,是否要阻止任何不完全是
foo
bar
的内容?还是要阻止包含这两个字符串的任何内容

它们是否可以是另一个字符串的一部分,如@Tim的
foonly
bartender
示例

我只想为每一种模式提出建议:

/^(?!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"

是不是有什么东西不是“foo”或“bar”?或者任何不同时包含“foo”和“bar”的字符串?如何处理包含这两个单词中的一个以及其他字符的字符串?例如,应该允许
foo-blah-blah-foo-blah
吗?这将无法匹配
foonly
bartener
(甚至任何只包含
foo
bar
的单词),而这不是goombaloon想要的。@Tim Pietzcker:不同的解释。如果他想扩展他提到的问题的答案,那么这是可行的:它不会匹配任何包含“foo”或“bar”或两者的内容。这只会在整个字符串是其中一个单词时触发。不清楚OP是否想要这样,但值得指出。这是对问题第一部分的一个很好的回答:“一个正则表达式,可以匹配任何非foo和bar的内容。”
/^(?!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"