您正在使用捕获组,并在使用捕获组
.findall()时改变其行为(它只会返回捕获组的内容)。您的正则表达式可以简化,但是如果您使用 非 捕获组,则您的版本可以使用:
L = re.findall(r"(?:http|https)://[^/"]+/", site_str)
如果在表达式周围使用单引号,则不需要转义双引号,并且只需更改
s表达式中的,因此
s?也可以工作:
L = re.findall(r'https?://[^/"]+/', site_str)
演示:
>>> import re>>> example = '''... "http://someserver.com/"... "https://anotherserver.com/with/path"... '''>>> re.findall(r'https?://[^/"]+/', example)['http://someserver.com/', 'https://anotherserver.com/']



