您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python正则表达式替代

Python正则表达式替代

您正在使用捕获组,并在使用捕获组.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/']
python 2022/1/1 18:27:57 有173人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶