匹配字符串的Python正则表达式

2024-04-19 05:30:36 发布

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

我是新来的。只是Python正则表达式有问题。 我有一根弦

http://sample.com/test/id549268848?at=5
http://sample.com/test/id621311331?at=5
...

找不到正确的方法,只能在身份证后获得号码,并在身份证后剥夺一切。 像这样从循环中调用

self.splitAppId(rec[4])

其中rec[4]URL来自列表

函数本身:

def splitAppId(self, url):
        idMatch = re.search('/id(.*)?$', url)
        return idMatch

Tags: sample方法testselfcomhttpurlat
2条回答

你做错了。应该是这样的:

 def splitAppId(self, url):
        idMatch = re.search(r'/id([^/]+)\?[^/]*$', url)
        return idMatch.group(1)

如果URL是format,那么

>>> "http://sample.com/test/id549268848?at=5".split('id')[-1].split('?')[0]
'549268848'

相关问题 更多 >