繁体   English   中英

Python正则表达式与lookbehind和lookahead无法正常工作

[英]Python regex with lookbehind and lookahead not working

我有一个Python regexdebuggex中工作得很好:

在此输入图像描述

但是,当我在Python控制台中执行此操作时:

import re
rgx = re.compile(r'(?<="careerJobAdsList", ){"jobAds":.*}](?=,"nodes":)')

st = 'widget("careerJobAdsList", {"jobAds":[{"id":607}],"nodes":[{"id":2,"parent_id"'

rgx.match(st)
>>> None

我试图逃避rgx所有特殊字符,但它不会改变输出。

我错过了什么吗?

match在字符串的开头找到匹配项。 请改用search

print(rgx.search(st))

引用re.match

如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的MatchObject实例。 如果字符串与模式不匹配,则返回None; 请注意,这与零长度匹配不同。

Python代码

import re
rgx = re.compile(r'(?<="careerJobAdsList", ){"jobAds":.*}](?=,"nodes":)')
st = 'widget("careerJobAdsList", {"jobAds":[{"id":607}],"nodes":[{"id":2,"parent_id"'
print(rgx.search(st))

暂无
暂无

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

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