python 匹配amazon链接正则表达式并替换它

beq87vna  于 2023-01-01  发布在  Python
关注(0)|答案(2)|浏览(103)

我试图创建一个正则表达式,匹配字符串上的亚马逊链接,并将其替换为另一个字符串。我写的代码暂时不工作,因为它只是替换了网址的一部分。我想替换所有的网址。这是代码

import re
regex = r"https://amzn.to/[a-zA-Z0-9]+" + "|" + r"https://www.amazon.it/[a-zA-Z0-9]+" 

string="https://amzn.to/3Ueforw"
string1="https://www.amazon.it/dp/B08F9LM1FB/?tag=seller050-21&psc=1"
string = re.sub(regex, "URL", string)
string1 = re.sub(regex, "URL", string1)
print(string)
print(string1) # here I want to URL too not "URL/other part of the url"
hpcdzsge

hpcdzsge1#

我试着更新你的正则表达式,只是为了解释-我已经写了正则表达式,其中包括亚马逊域使用非贪婪与任何字符匹配与单行

import re
regex = r"(https://amzn\.to/.*)|(https://www.amazon\.it/.*)"

string="https://amzn.to/3Ueforw"
string1="https://www.amazon.it/dp/B08F9LM1FB/?tag=seller050-21&psc=1"
string = re.sub(regex, "URL", string)
string1 = re.sub(regex, "URL", string1)
print(string)enter code here
print(string1) # here I want to URL too not "URL/other part of the url"
rkue9o1l

rkue9o1l2#

将regex语句替换为以下内容:

regex = r"https://amzn.to/[a-zA-Z0-9]+" + "|" + r"https://www.amazon.it/.*"

正则表达式https://www.amazon.it/.*表示https://www.amazon.it/之后的任何字符。

演示

import re
regex = r"https://amzn.to/[a-zA-Z0-9]+" + "|" + r"https://www.amazon.it/.*"

string="https://amzn.to/3Ueforw"
string1="https://www.amazon.it/dp/B08F9LM1FB/?tag=seller050-21&psc=1"
string = re.sub(regex, "URL", string)
string1 = re.sub(regex, "URL", string1)
print(string)
print(string1) # here I want to URL too not "URL/other part of the url"

输出

URL
URL

相关问题