简体   繁体   中英

Python regex to match string

I'm new here. Just having a problem with Python regex. I have a strings like

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

Can't find the right way to get only number after id and strip everything after. Calling from the loop like this

self.splitAppId(rec[4])

Where rec[4] URL from list

The function itself:

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

you doing wrong. Should be like this:

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

If URL is format, then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM