简体   繁体   中英

Python regex with lookbehind and lookahead not working

I have a Python regex working finely in debuggex :

在此输入图像描述

However, when I do this in a Python console:

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

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

rgx.match(st)
>>> None

I've tried to escape all special characters in rgx , but it doesn't change the output.

Am I missing something?

match finds match in beginning of string. Use search instead

print(rgx.search(st))

Quoting re.match

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.

Python Code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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