我想只选择剪切链接区域("https://cutt.ly/XJDV1G8")我尝试过RegEx,但没有成功。在代码下面,我尝试选择(")区域,但我需要选择第8个(")区域。
import re
text = '{"url":{"status":7,"fullLink":"https://twitter.com/furkan_alkaya_","date":"2022-06-08","shortLink":"https://cutt.ly/XJDV1G8","title":"Furkan Alkaya (@furkan_alkaya_) | Twitter"}}'
x = re.search(r"b""w+",text)
print(x.group())
你拥有的是JSON,所以你应该使用JSON库来处理它:
import json
text = '{"url":{"status":7,"fullLink":"https://twitter.com/furkan_alkaya_","date":"2022-06-08","shortLink":"https://cutt.ly/XJDV1G8","title":"Furkan Alkaya (@furkan_alkaya_) | Twitter"}}'
d = json.loads(text)
d['url']['shortLink']
输出:
'https://cutt.ly/XJDV1G8'
使用正则表达式
x = re.search(r'("shortLink":")(w{5}:.{20})', text)
x[2]
这将选择第8个("…")包括空("):
match = re.search(r'(?:"([^"]*)".+?){8}', text)
value = match[1] if match else None
print( value )