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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6830796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:45:43  来源:igfitidea点击:

Regex to match anything but two words

.netregexregex-negation

提问by goombaloon

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”的任何东西。我发现如何在正则表达式中匹配除一个单词以外的任何内容以匹配不包含单词的行?但我对正则表达式不是很熟练,并且不确定如何在此标准中添加第二个单词。

Any help would be most appreciated!

非常感激任何的帮助!

CLARIFICATION:

澄清:

I wanted to match on anything that wasn't EXACTLY foo or bar.

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

回答by Tim Pietzcker

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.Singlelineif your string can contain newlines that you also wish to match.

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

回答by Pindatjuh

Answer to the question: "How to add a second word to this critera?"

回答这个问题:“如何在第二个单词添加到这个性判据?”

The answer of the question you link to is:

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

^((?!word).)*$

Where (?!word)is a negative look-ahead. The answer for this question is:

哪里(?!word)有负面展望。这个问题的答案是:

^((?!wordone|wordtwo).)*$

Works for both words. Note: you should enable the globaland multilineoptions, 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 answerfor a detailed explanation.

有关详细说明,请参阅此答案

回答by Justin Morgan

I get what you want to do, but the specificsof what you want to block/allow are a little unclear. For example, do you want to block anything that isn't exactlyfooor bar? Or do you want to block anything containingthose two strings?

我明白你想要做什么,但你想要阻止/允许的具体细节有点不清楚。例如,您要阻止任何不完全foo或不正确的内容bar吗?还是要阻止包含这两个字符串的任何内容?

Can they be part of another string, as in @Tim's foonlyor bartenderexamples?

它们可以是另一个字符串的一部分,如@Timfoonlybartender示例中那样吗?

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"