【发布时间】:2019-08-09 13:43:19
【问题描述】:
我想识别介于两个模式之间的字符串(例如lettre/ 和 ")。此外,识别的字符串不应对应于第三个模式(例如somth?other)。
在 MAC OSX 10.13 上运行的 Python 3.7
import re
strings = ['lettre/abc"','lettre/somth?other"','lettre/acc"','lettre/edf"de','lettre/nhy"','lettre/somth?other"']
res0_1 = re.search('lettre/.*?\"', strings[0])
res1_1 = re.search('lettre/.*?\"', strings[1])
print(res0_1)
<re.Match object; span=(0, 11), match='lettre/abc"'>
print(res1_1)
<re.Match object; span=(0, 19), match='lettre/somth?other"'>
res0_2 = re.search('lettre/(.*?\"&^[somth\?other])', strings[0])
res1_2 = re.search('lettre/(.*?\"&^[somth\?other])', strings[1])
print(res0_2)
None
print(res1_2)
None
我想为strings[0] 获取res0_1,为strings[1] 获取res1_2。
【问题讨论】:
-
你想说什么?如果
/的右侧有somth?other,您不想匹配?试试r'lettre/(?!somth\?other)[^"]*"'。见the regex demo。 -
@WiktorStribiżew 这正是我所需要的,谢谢。
标签: python regex python-3.x search