简体   繁体   English

正则表达式为字母,后跟空格和引号(“)

[英]Regex for a letter followed by space and quotations ( ")

I'm iterating over a list of JSON objects and want to find every occurrence of any letter followed by a space and then a quotation mark so that: 我正在遍历JSON对象的列表,希望找到每个字母的所有出现,然后是一个空格,然后是一个引号,以便:

MATCH: "Some words here " 比赛: "Some words here "

NO MATCH: "Some words here" 没有匹配: "Some words here"

This is what I'm trying but it does not work: 这是我正在尝试的方法,但是不起作用:

for i in range (len(json_list)):
     m = re.search('(?=[a-z])(*\s)(?=\")', json_list[i])
     print (m.group(0))

Failing as such: 这样失败:

Traceback (most recent call last):
  File "api_extraspace.py", line 13, in <module>
    print (m.group(0))
AttributeError: 'NoneType' object has no attribute 'group'
  1. Your lookbehind is missing the less-than sign: (?=[az]) -> (?<=[az]) 您的后方遗漏了小于号: (?=[az]) -> (?<=[az])
  2. (*\\s) is invalid. (*\\s)无效。 I think you want \\s+ . 我认为您想要\\s+

Here's a working example: 这是一个工作示例:

import re
for s in ['"Some words here"', '"Some words here "']:
    m = re.search('(?<=[a-z])\s+(?=")', s)
    print(repr(s), m)

Output: 输出:

'"Some words here"' None
'"Some words here "' <_sre.SRE_Match object; span=(16, 17), match=' '>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM