如何否定正则表达式中的特定单词? [英] How to negate specific word in regex?
本文介绍了如何否定正则表达式中的特定单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以像 [^bar]
那样否定一组字符,但我需要一个正则表达式,其中否定适用于特定的单词 - 所以在我的例子中,我如何否定一个实际的 bar
,而不是bar 中的任何字符"?
解决方案
一个很好的方法是使用 负前瞻:
^(?!.*bar).*$
<块引用>
否定前瞻结构是一对括号,左括号后跟一个问号和一个感叹号.在前瞻内部 [是任何正则表达式模式].
I know that I can negate group of chars as in [^bar]
but I need a regular expression where negation applies to the specific word - so in my example how do I negate an actual bar
, and not "any chars in bar"?
解决方案
A great way to do this is to use negative lookahead:
^(?!.*bar).*$
The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point. Inside the lookahead [is any regex pattern].
这篇关于如何否定正则表达式中的特定单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文