我有一個包含鍵值對的網路廢棄字串,即firstName:"Quaran", lastName:"McPherson"
st = '{"accountId":405266,"firstName":"Quaran","lastName":"McPherson","accountIdentifier":"StudentAthlete","profilePicUrl":"https://pbs.twimg.com/profile_images/1331475329014181888/4z19KrCf.jpg","networkProfileCode":"quaran-mcpherson","hasDeals":true,"activityMin":11,"sports":["Men\'s Basketball","Basketball"],"currentTeams":["Nebraska Cornhuskers"],"previousTeams":[],"facebookReach":null,"twitterReach":619,"instagramReach":0,"linkedInReach":null},{"accountId":375964,"firstName":"Micole","lastName":"Cayton","accountIdentifier":"StudentAthlete","profilePicUrl":"https://opendorsepr.blob.core.windows.net/media/375964/20220622223838_46dbe3fd-a683-436b-84d4-90c84a5af35f.jpg","networkProfileCode":"micole-cayton","hasDeals":true,"activityMin":16,"sports":["Basketball","Women\'s Basketball"],"currentTeams":["Minnesota Golden Gophers"],"previousTeams":["Cal Berkeley Golden Bears"],"facebookReach":0,"twitterReach":1273,"instagramReach":5700,"linkedInReach":null}'
我正在嘗試以串列格式從該字串中提取 first_name、last_name 和其他一些引數,這樣我將有一個 first_name 串列,其中包含字串中的所有 first_names
我嘗試使用re.findall('"firstName":'"(.*)\S$",st)訪問文本"Quaran",但結果采用以下格式
'"Quaran","lastName":"McPherson","accountIdentifier":"StudentAthlete","profilePicUrl":"https://pbs.twimg.com/profile_images/1331475329014181888/4z19KrCf.jpg","networkProfileCode":"quaran-mcpherson","hasDeals":true,"activityMin":11,"sports":["Men\'s Basketball","Basketball"],"currentTeams":["Nebraska Cornhuskers"],"previousTeams":[],"facebookReach":null,"twitterReach":619,"instagramReach":0,"linkedInReach":null}
如何結束正則運算式中的指定以在引號中的名稱末尾結束搜索?
TIA
uj5u.com熱心網友回復:
試試這個正則運算式(?<=\"firstName\":\").*?(?=\")。這 ?在中間使它成為惰性匹配,因此它一找到 " 字符就停止匹配。
uj5u.com熱心網友回復:
您的字串似乎是 JSON 陣列,如果有效,您可以輕松地用任何語言決議 json。要使您的字串有效,首先在字串的最后添加“[”,然后在字串的最后添加“]”,然后用您的語言決議 JSON。如
腳本:
JSON.parse(st)
Python:
import json
dict = json.loads(st)
正則運算式:
如果您嚴格希望使用正則運算式進行決議,請使用:
/(?:\"|\')(?<key>[\w\d] )(?:\"|\')(?:\:\s*)(?:\"|\')?(?<value>[\w\s-]*)(?:\"|\')?/gm
uj5u.com熱心網友回復:
嘗試這個:
(?<="firstName":")[^"\r\n]
(?<="firstName":")轉到"firstName":"字串中出現的位置,
[^"\r\n] "然后匹配除,\r和之外的一個或多個字符\n。不要越過 firstName 值的第二個雙引號,也不要越過任何換行符。
請參閱正則運算式演示。
請參閱python 演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/533758.html
