正则表达式匹配模式,但排除一组单词 [英] Regex to match a pattern, but exclude a set of words
问题描述
我一直在查看 SO,虽然这个问题已经在一种情况下得到了回答:
I have been looking through SO and although this question has been answered in one scenario:
这不是我想要的.我正在尝试编写一个正则表达式,它匹配 [\w]+[(] 形式的任何字符串,但它不匹配三个字符串cat("、dog("和sheep(").特别是.
It's not quite what I'm looking for. I am trying to write a regular expression which matches any string of the form [\w]+[(], but which doesn't match the three strings "cat(", "dog(" and "sheep(" specifically.
我一直在玩向前看和向后看,但我不能完全到达那里.我可能使这个问题过于复杂,因此我们将不胜感激.
I have been playing with lookahead and lookbehind, but I can't quite get there. I may be overcomplicating this, so any help would be greatly appreciated.
推荐答案
如果正则表达式实现支持 前瞻或后视断言,您可以使用以下内容:
If the regular expression implementation supports look-ahead or look-behind assertions, you could use the following:
使用否定前瞻断言:
Using a negative look-ahead assertion:
\b(?!(?:cat|dog|sheep)\()\w+\(
使用否定的后视断言:
Using a negative look-behind assertion:
\b\w+\((?<!\b(?:cat|dog|sheep)\()
我添加了标记 单词边界的 \b
锚点一>.所以 catdog(
将被匹配,尽管它包含 dog(
.
I added the \b
anchor that marks a word boundary. So catdog(
would be matched although it contains dog(
.
但是,虽然正则表达式实现更广泛地支持前瞻断言,但带有后视断言的正则表达式更有效,因为它只测试前面的正则表达式(在我们的例子中\b\w+\(
) 已经匹配.然而,先行断言将在之前测试实际正则表达式匹配.所以在我们的例子中,只要 \b代码> 匹配.
But while look-ahead assertions are more widely supported by regex implementations, the regex with the look-behind assertion is more efficient since it’s only tested if the preceding regex (in our case \b\w+\(
) already did match. However the look-ahead assertion would be tested before the actual regex would match. So in our case the look-ahead assertion is tested whenever \b
is matched.
这篇关于正则表达式匹配模式,但排除一组单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!