【发布时间】:2021-09-30 10:38:48
【问题描述】:
import regex
frase = "text https://www.gamivo.com/product/sea-of-thieves-pc-xbox-one other text https://www.gamivo.com/product/fifa-21-origin-eng-pl-cz-tr"
x = regex.findall(r"/((http[s]?:\/\/)?(www\.)?(gamivo\.com\S*){1})", frase)
print(x)
结果:
[('www.gamivo.com/product/sea-of-thieves-pc-xbox-one', '', 'www.', 'gamivo.com/product/sea-of-thieves-pc-xbox-one'), ('www.gamivo.com/product/fifa-21-origin-eng-pl-cz-tr', '', 'www.', 'gamivo.com/product/fifa-21-origin-eng-pl-cz-tr')]
我想要类似的东西:
[('https://www.gamivo.com/product/sea-of-thieves-pc-xbox-one', 'https://gamivo.com/product/fifa-21-origin-eng-pl-cz-tr')]
我该怎么做?
【问题讨论】:
-
删除第一个
/并使用非捕获组。r'(?:https?://)?(?:www\.)?gamivo\.com\S*',见this demo。 -
你真的需要正则表达式吗?在空格上拆分并在结果数组中使用带有 https 的空格
-
@leoOrion 是的,它适用于需要正则表达式的更大项目。所以在最终项目中,我将替换为 str.replace() 以使用短链接
标签: python regex python-regex