簡體   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