我正在遍历JSON对象列表,并希望查找每个字母的每个出现位置,后跟一个空格,再加上一个引号,以便:
MATCH:
"Some words here "
没有比赛:
"Some words here"
这是我正在尝试的方法,但是不起作用:
for i in range (len(json_list)):
m = re.search('(?=[a-z])(*\s)(?=\")', json_list[i])
print (m.group(0))
失败的原因:
Traceback (most recent call last):
File "api_extraspace.py", line 13, in <module>
print (m.group(0))
AttributeError: 'NoneType' object has no attribute 'group'
答案 0 :(得分:2)
(?=[a-z])
-> (?<=[a-z])
(*\s)
无效。我认为您想要\s+
。这是一个可行的示例:
import re
for s in ['"Some words here"', '"Some words here "']:
m = re.search('(?<=[a-z])\s+(?=")', s)
print(repr(s), m)
输出:
'"Some words here"' None
'"Some words here "' <_sre.SRE_Match object; span=(16, 17), match=' '>