簡體   English   中英

Python正則表達式匹配字符串

[英]Python regex to match string

我是新來的。 Python正則表達式有問題。 我有一個像

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

無法找到正確的方法來在id之后僅獲取數字並在其后剝離所有內容。 像這樣從循環調用

self.splitAppId(rec[4])

清單中的rec [4]網址

函數本身:

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

你做錯了。 應該是這樣的:

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

如果網址是格式,則

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM