因此,我尝试匹配所有出现的某个缩写XYZ
,但前提是当它们的两边都没有用括号包围时。字符串XYZ filler text
,(XYZ filler text)
和(Filler text XYZ)
应该都匹配,而字符串(XYZ)
不应该匹配。
我能得到的最接近的是
(?<!\()XYZ(?!\))
当然,这样做的问题是,如果任何一个否定断言都匹配,那么整个事物将不再匹配。我应该怎么做?谢谢!
答案 0 :(得分:3)
您可以或(|
)正则表达式:
(?<!\()XYZ|XYZ(?!\))
示例:
import re
lst = ['XYZ filler text', '(XYZ filler text)', '(Filler text XYZ)', '(XYZ)']
for x in lst:
print(re.search(r'(?<!\()XYZ|XYZ(?!\))', x))
输出:
<re.Match object; span=(0, 3), match='XYZ'>
<re.Match object; span=(1, 4), match='XYZ'>
<re.Match object; span=(13, 16), match='XYZ'>
None
答案 1 :(得分:1)
我会使用正则表达式#1或#2
并避免使用正则表达式#3
Regex1: XYZ(?!(?<=\(...(?=\))))
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 9
Elapsed Time: 0.53 s, 531.79 ms, 531788 µs
Matches per sec: 846,201
Regex2: XYZ(?:(?!\))|(?<!\(...))
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 9
Elapsed Time: 0.53 s, 533.03 ms, 533029 µs
Matches per sec: 844,231
Regex3: (?<!\()XYZ|XYZ(?!\))
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 9
Elapsed Time: 1.42 s, 1417.15 ms, 1417151 µs
Matches per sec: 317,538