gpt4 book ai didi

python - 用于在 Python 中匹配 URL 的正则表达式

转载 作者:太空宇宙 更新时间:2023-11-04 01:59:09 25 4
gpt4 key购买 nike

我有这个示例字符串:

line = '[text] something - https://www.myurl.com/test1/ lorem ipsum https://www.myurl.com/test2/ - https://www.myurl.com/test3/ marker needle - some more text at the end'

我需要在“marker needle”之前提取路径(没有斜线)。以下用于列出所有路径:

print re.findall('https://www\\.myurl\\.com/(.+?)/', line)
# ['test1', 'test2', 'test3']

但是,当我将它更改为只找到我想要的路径(“标记针”之前的路径)时,它会给出一个奇怪的输出:

print re.findall('https://www\\.myurl\\.com/(.+?)/ marker needle', line)
# ['test1/ lorem ipsum https://www.myurl.com/test2/ - https://www.myurl.com/test3']

我的预期输出:

test3

我用 re.search 做了同样的尝试,但结果是一样的。

最佳答案

这个表达式有三个捕获组,其中第二个有我们想要的输出:

(https:\/\/www.myurl.com\/)([A-Za-z0-9-]+)(\/\smarker needle)

This tool如果您愿意,可以帮助我们修改/更改表达式。

enter image description here

正则表达式描述图

jex.im可视化正则表达式:

enter image description here

Python 测试

# -*- coding: UTF-8 -*-
import re

string = "[text] something - https://www.myurl.com/test1/ lorem ipsum https://www.myurl.com/test2/ - https://www.myurl.com/test3/ marker needle - some more text at the end"
expression = r'(https:\/\/www.myurl.com\/)([A-Za-z0-9-]+)(\/\smarker needle)'
match = re.search(expression, string)
if match:
print("YAAAY! \"" + match.group(2) + "\" is a match 💚💚💚 ")
else:
print('🙀 Sorry! No matches!')

输出

YAAAY! "test3" is a match 💚💚💚

性能测试

此代码段返回 100 万次 for 循环的运行时间。

const repeat = 10;
const start = Date.now();

for (var i = repeat; i >= 0; i--) {
const regex = /(.*)(https:\/\/www.myurl.com\/)([A-Za-z0-9-]+)(\/\smarker needle)(.*)/gm;
const str = "[text] something - https://www.myurl.com/test1/ lorem ipsum https://www.myurl.com/test2/ - https://www.myurl.com/test3/ marker needle - some more text at the end";
const subst = `$3`;

var match = str.replace(regex, subst);
}

const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match 💚💚💚 ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. 😳 ");

关于python - 用于在 Python 中匹配 URL 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56094599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com