[英]Regex match if not before and after
How can I match 'suck' only if not part of 'honeysuckle'? 如果不是'金银花'的一部分,我怎么能匹配'吮吸'?
Using lookbehind and lookahead I can match suck if not 'honeysuck' or 'suckle', but it also fails to catch something like 'honeysucker'; 使用lookbehind和lookahead我可以匹配suck如果不是'honeysuck'或'suckle',但它也不能捕捉像'honeysucker'的东西; here the expression should match, because it doesn't end in
le
: 这里表达式应该匹配,因为它不以
le
结尾:
re.search(r'(?<!honey)suck(?!le)', 'honeysucker')
You need to nest the lookaround assertions: 您需要嵌套环绕声断言:
>>> import re
>>> regex = re.compile(r"(?<!honey(?=suckle))suck")
>>> regex.search("honeysuckle")
>>> regex.search("honeysucker")
<_sre.SRE_Match object at 0x00000000029B6370>
>>> regex.search("suckle")
<_sre.SRE_Match object at 0x00000000029B63D8>
>>> regex.search("suck")
<_sre.SRE_Match object at 0x00000000029B6370>
An equivalent solution would be suck(?!(?<=honeysuck)le)
. 一个等价的解决方案是
suck(?!(?<=honeysuck)le)
。
here is a solution without using regular expressions: 这是一个不使用正则表达式的解决方案:
s = s.replace('honeysuckle','')
and now: 现在:
re.search('suck',s)
and this would work for any of these strings : honeysuckle sucks
, this sucks
and even regular expressions suck
. 这适用于任何这些字符串:
honeysuckle sucks
, this sucks
,甚至regular expressions suck
this sucks
。
I believe you should separate your exceptions in a different Array, just in case in the future you wish to add a different rule. 我相信你应该在不同的数组中分离你的异常,以防将来你希望添加不同的规则。 This will be easier to read, and will be faster in the future to change if needed.
这将更容易阅读,并且如果需要将来更快。
My suggestion in Ruby is: 我在Ruby中的建议是:
words = ['honeysuck', 'suckle', 'HONEYSUCKER', 'honeysuckle']
EXCEPTIONS = ['honeysuckle']
def match_suck word
if (word =~ /suck/i) != nil
# should not match any of the exceptions
return true unless EXCEPTIONS.include? word.downcase
end
false
end
words.each{ |w|
puts "Testing match of '#{w}' : #{match_suck(w)}"
}
>>>string = 'honeysucker'
>>>print 'suck' in string
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.