简体   繁体   中英

Extract all numbers from string that matches pattern

For example:

String = "geod rfff eef 234_1538 ffgg df 134774  234_1645"

I want to extract only 234_1538 and 234_1645 , not the remaining patterns. I tried using Re.search but it returns only the first match.

It's not clear exactly what you want to consider a "match" or not, so I've made the assumption to consider any continuous series of digits with an underscore somewhere in the middle a match.

To find all matches in a string, you can use re.findall . Here's a demo:

import re

s = "geod rfff eef 234_1538 ffgg df 134774  234_1645"

print(re.findall('\d+_\d+', s))

Output

['234_1538', '234_1645']

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