python - 正则表达式表示一个字母后跟空格和引号 (")
问题描述
我正在遍历一个 JSON 对象列表,并希望找到每个出现的任何字母,后跟一个空格,然后是一个引号,以便:
匹配:
"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'
解决方案
- 您的lookbehind缺少小于号:
(?=[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=' '>
推荐阅读
- verilog - 使用接口创建结构模块的问题(SystemVerilog)
- angular - Angular6 - FormControlName - TypeError:无法读取未定义的属性'get'
- java - gradle编译错误:找不到符号
- java - 如何使用 gradle 任务运行 shadow jar?
- java - 在文件 Java 中查找一定数量的子字符串
- twitter - tweepy 第一个程序:不识别 API 调用
- r - 如何在 R Studio 中将两个数据集合并为一个并用两个数据集绘制一张图
- algorithm - 二分图中边的均匀分布
- java - Java - 从 JPanel 扩展,在其中添加新面板
- linux - 将脚本加载到 /bin,然后删除,但仍然有效