正则表达式匹配任何东西,但两个词 [英] Regex to match anything but two words

查看:192
本文介绍了正则表达式匹配任何东西,但两个词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个正EX pression匹配任何不是富和栏。我发现了如何在<一个匹配什么,但一个字href="http://stackoverflow.com/questions/406230/regular-ex$p$pssion-to-match-string-not-containing-a-word">Regular EX pression匹配的字符串不包含一个字?但我不是很熟练的用正则表达式和我不确定如何将第二个字添加到此性判据。

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 string not containing a word? but I'm not very skilled with regex and am unsure of how to add a second word to this critera.

任何帮助将是非常美联社preciated!

Any help would be most appreciated!

澄清:

我想匹配任何不完全是富或酒吧。

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

推荐答案

问题的答案:?一个普通防爆pression匹配任何不是富和栏

^(?!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 如果你的字符串可以包含换行符,你也想匹配。

You might need to set RegexOptions.Singleline if your string can contain newlines that you also wish to match.

这篇关于正则表达式匹配任何东西,但两个词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆