我有一个如下格式的资源:
{"url": "http://res1.icourses.cn/share/process17//mp4/2017/3/17/6332c641-28b5-43a0-894c-972bd804f4e1_SD.mp4", "name": "1-课程导学"},
{"url": "http://res2.icourses.cn/share/process17//mp4/2017/3/17/a21902b6-8680-4bdf-8f47-4f99d1354475_SD.mp4", "name": "2-计算机网络的定义与分类"}
我想从url中提取文件名6332c641-28b5-43a0-894c-972bd804f4e1_SD.mp4
和{
如何编写正则表达式来匹配此位置的字符串?在
基于您提供的字符串,您可以迭代字典,获取“url”的值并使用以下regex
([^\/]*)$
说明:
例如:
^{pr2}$在本例中,我们利用了文件名出现在字符串末尾的事实。使用
$
锚点使唯一有效的匹配终止字符串。在如果您想对作为字符串转换的字典执行此操作,则可以通过更改结束条件来。就像这样
([^\/]*?)\",
。现在",
终止匹配(注意\
来转义"
。见https://regex101.com/r/k9VwC6/25最后,如果我们没有那么幸运,捕获组在字符串的末尾(这意味着我们不能使用
$
),我们可以使用一个否定的后面看。你可以读一读那些here。在您可以使用短regex
[^/]*$
代码:
输出:
检查regex-https://regex101.com/r/k9VwC6/30
您可以使用
re.findall
:输出:
^{pr2}$相关问题 更多 >
编程相关推荐