正则表达式否定组 [英] regular expression negative group
问题描述
我正在尝试在正则表达式中匹配和捕获包含ball"的任何单词,而其前面没有foot"或basket".例如,我想匹配排球"和躲避球"而不是篮球"或足球".重要的是我不能使用一个积极的群体,而是一个消极的群体.我试过的:
I'm trying, in a regular expression, to match and capture any word that contains "ball" without having "foot" or "basket" in front of it. for example, I want to match "volleyball" and "dodgeball" but not "basketball" or "football". important thing is that I can't use a positive group but a negative one. what I tried:
[^(?:foot|basket)(ball)]
!(?:foot|basket)(ball)
找到相反的东西相当简单:
finding the opposite is rather simple:
(?:foot|basket)(ball)
但这不是我要找的.我需要反过来.
but that's not what I'm looking for. I need it the other way around.
这是 php,它是一个preg_replace"命令.
this is php, it's a "preg_replace" command.
推荐答案
PHP 使用 PCRE.因此,可以使用负向后视语法:
PHP uses PCREs. Thus, negative lookbehind syntax is available:
(?<!foot|basket)ball
这篇关于正则表达式否定组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!