使用re.findall在URL末尾提取id

2024-05-23 16:36:10 发布

您现在位置:Python中文网/ 问答频道 /正文

sfdc_url= 'https://unify.my.salesforce.com/a0n82000006VlNaPPP'

sfdc_partnerid= re.findall(r'https://unify.my.salesforce.com/(.*?)?sr',sfdc_url)

我要sfdc\u partnerid返回“a0n82000006VlNaPPP”

我总是得到一个空的[]结果


Tags: httpsrecomurlmysfdcsalesforcesr
2条回答

如果你在python3上,你也可以看看urllib.parse

例如

from urllib import urlparse
result = urlparse("https://unify.my.salesforce.com/a0n82000006VlNaPPP")
result.path

给定您的url,正确的正则表达式应该是:

sfdc_url= 'https://unify.my.salesforce.com/a0n82000006VlNaPPP'
sfdc_partnerid = re.findall(r'https://unify.my.salesforce.com/(.*)', sfdc_url)
print sfdc_partnerid

也可以在https://eval.in/1078060上检查

关于量词: *?是一个惰性量词,匹配的字符尽可能少

(.*?)实际上意味着“尽可能少地匹配零和无限之间的任何内容”,这是空的

在那之后加上?,意味着“前一个匹配的零或一个”,这样: (.*?)?实际上意味着“零或一无所有”

我不确定原始regex中剩余的sr

相关问题 更多 >