简体   繁体   中英

Python3 Regex for extract digits

I try to extract digits from urls with Python.

import re

url = '/randomtext/02@randomtext/01@randomtext/03@randomtext/01@.mp4'
for m in re.finditer(r'\d{2}', url):
    print(m[0], end='')

print function do this but I need the same result in variable, how can I did this?

s = "".join(m[0] for m in re.finditer(...))

Just use re.sub with an empty string as the replacement. That pattern should be fine.

url = '/randomtext/02@randomtext/01@randomtext/03@randomtext/01@.mp4'
print(re.sub(r'\d{2}', '', url))

Prints:

/randomtext/@randomtext/@randomtext/@randomtext/@.mp4

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