user6241246 user6241246 - 1 year ago 59
Python Question

Find substrings in string using python

I have data

I need to get all url, where are numbers after
If I use

if '/id' in url:

it returns all strings.
How can I return only
with number?

Answer Source

you can use re module. for example:

import re
s = """
p = re.compile('/id\d+')
print p.findall(s)

The output will be:

['/id211452033', '/id211452033', '/id165402000']

PS: if want to remove / in the result, just update regular expression to /(id\d+). that is because, findall just returns the captured groups