正则表达式 - 从 url 中提取代码 [已关闭]

Regular expression - extract code from url [closed]

提问人:Rodrigo 提问时间:11/15/2023 最后编辑:marc_sRodrigo 更新时间:11/15/2023 访问量:50

问:


想改进这个问题吗?更新问题,使其仅通过编辑这篇文章来关注一个问题。

7天前关闭。

我在 json 中有以下 url,我只想检索之后的部分/

"url":"www.mysite.com/4c344ce1-c6b5-497a-a8da-f4dfbc9f3668"

我的正则表达式:

"url":"[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"

只是它不起作用,它不只是抓住 ./

这是怎么回事,所以它没有抓住我想要验证的部分?

正则表达式

评论

1赞 Wiktor Stribiżew 11/15/2023
使用 JSON 库解析 JSON。
0赞 Rodrigo 11/15/2023
{ “mysite”: { “url”: “www.mysite.com/4c344ce1-c6b5-497a-a8da-f4dfbc9f3668”, “id”: 45249, } }
0赞 David 11/15/2023
“only the part after /” - 你真的需要正则表达式吗?从 JSON 中检索字符串值后,将其拆分为字符,并从生成的集合中获取最后一个元素。/
0赞 Rodrigo 11/15/2023
表达式应仅在 / Exameple: www.mysite.com/4c344ce1-c6b5-497a-a8da-f4dfbc9f3668 返回: 4c344ce1-c6b5-497a-a8da-f4dfbc9f3668 之后返回哈希值
0赞 Rodrigo 11/15/2023
我不能使用拆分,因为我正在使用程序来测试JMeter

答:

-1赞 Jado 11/15/2023 #1

URL 之后的所有内容 /

如果您只想要该特定 URL 的 .com/ 之后的所有内容 - 这是您要问的,但可能不是您真正想要的,请执行以下操作:

import json

json_data = '{"url":"www.mysite.com/4c344ce1-c6b5-497a-a8da-f4dfbc9f3668"}'

data = json.loads(json_data)
pattern = r'com\/(.*)'

match = re.search(pattern, data['url'])

# Check if the pattern is found
if match:
    # Access the captured group using group(1)
    uuid_group = match.group(1)
    print("Pattern found:", uuid_group)
else:
    print("Pattern not found")

提取 url 中的第一个 uuid

pattern = r'[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}'

match = re.search(pattern, data['url'])

# Check if the pattern is found
if match:
    # Access the captured group using group(1)
    uuid_group = match.group(0)
    print("Pattern found:", uuid_group)
else:
    print("Pattern not found")

0赞 Reilas 11/15/2023 #2

请尝试以下捕获模式

\"url\"\s*:\s*\".+?/(.+?)(?<!\\)\"