Python 如何通过正则表达式提取URI列表?

Python 如何通过正则表达式提取URI列表?,python,regex,Python,Regex,在python代码中,我从文本文件中获取字符串,如: a = "[{'index': '1', 'selected': 'true', 'length': '0', 'completedLength': '0', 'path': '', 'uris': [{'status': 'used', 'uri': 'http://www.single.com'}]}]" b ="[{'index': '1', 'selected': 'true', 'length': '0', 'completed

在python代码中,我从文本文件中获取字符串,如:

a =  "[{'index': '1', 'selected': 'true', 'length': '0', 'completedLength': '0', 'path': '', 'uris': [{'status': 'used', 'uri': 'http://www.single.com'}]}]"

b ="[{'index': '1', 'selected': 'true', 'length': '0', 'completedLength': '0', 'path': '', 'uris': [{'status': 'used', 'uri': 'http://www.mirrors.com'}, {'status': 'used', 'uri': 'http://www.mirrors2.com'}]}]"

c ="[{'index': '1', 'selected': 'true', 'length': '103674793', 'completedLength': '0', 'path': '/home/dr/Maher_Al-Muaiqly_(MP3_Quran)/002.mp3', 'uris': []}, {'index': '2', 'selected': 'true', 'length': '62043128', 'completedLength': '0', 'path': '/home/dr/Maher_Al-Muaiqly_(MP3_Quran)/004.mp3', 'uris': []}, {'index': '3', 'selected': 'true', 'length': '57914945', 'completedLength': '0', 'path': '/home/dr/Maher_Al-Muaiqly_(MP3_Quran)/003.mp3', 'uris': []}]"
我想获取值uri的文本,输出应该如下所示:

a = [{'status': 'used', 'uri': 'http://www.single.com'}] 

b = [{'status': 'used', 'uri': 'http://www.mirrors.com'}, {'status': 'used', 'uri': 'http://www.mirrors2.com'}]

c = [[],[],[]]
我花了很多时间在失败的试验中,通过使用字符串函数得到这个结果

uris = str.split('}, {')
for uri in uris :
     uri = uri.split(',')
     # and so on ...

但是,它的工作非常糟糕,尤其是在第二种情况下,我希望任何人都可以通过regex或任何其他方式来完成

a = a.replace(/^.*uris[^[]*(\[[^\]]*\]).*$/, '\1');
如果php是这样的话

$a = preg_replace('/^.*uris[^[]*(\[[^\]]*\]).*$/', '\1', $a);

编辑:我明白了,它不能完成“c”的全部任务。-

它们都是python文本。你可以用。不需要使用正则表达式

>>> a =  "[{'index': '1', 'selected': 'true', 'length': '0', 'completedLength': '0', 'path': '', 'uris': [{'status': 'used', 'uri': 'http://www.single.com'}]}]"
>>> b = "[{'index': '1', 'selected': 'true', 'length': '0', 'completedLength': '0', 'path': '', 'uris': [{'status': 'used', 'uri': 'http://www.mirrors.com'}, {'status': 'used', 'uri': 'http://www.mirrors2.com'}]}]"
>>> c = "[{'index': '1', 'selected': 'true', 'length': '103674793', 'completedLength': '0', 'path': '/home/dr/Maher_Al-Muaiqly_(MP3_Quran)/002.mp3', 'uris': []}, {'index': '2', 'selected': 'true', 'length': '62043128', 'completedLength': '0', 'path': '/home/dr/Maher_Al-Muaiqly_(MP3_Quran)/004.mp3', 'uris': []}, {'index': '3', 'selected': 'true', 'length': '57914945', 'completedLength': '0', 'path': '/home/dr/Maher_Al-Muaiqly_(MP3_Quran)/003.mp3', 'uris': []}]"


您使用的是什么语言?看起来像python列表文字。不是吗?@tabebqena,
c
是无效的;包含
近端。输入错误,我已更正。@tabebqena阅读您使用的标记文本(用鼠标悬停)。非常好,非常符合我的需要。
>>> import ast
>>> [x['uris'] for x in ast.literal_eval(a)]
[[{'status': 'used', 'uri': 'http://www.single.com'}]]
>>> [x['uris'] for x in ast.literal_eval(b)]
[[{'status': 'used', 'uri': 'http://www.mirrors.com'}, {'status': 'used', 'uri': 'http://www.mirrors2.com'}]]
>>> [x['uris'] for x in ast.literal_eval(c)]
[[], [], []]