简体   繁体   中英

python regex - extracting digits from string with numbers and characters

I have data that has strings like 'ms2p5', 'ms3', 'ms10' for which I need to extract the digits as convert to numbers as follows.

'ms2p5' => 2.5
'ms3' => 3
'ms10' => 10

I tried the below regex and it is able to get the match. One issue is with values having a character in the middle of the extracted string like '2p5'. What is the right approach to have a generic function that handles all these cases well while converting them into numeric values?

import re
re.search(r'\d+[p]*\d*', str).group() 

You could write an extraction function that searched for a numeric value (with or without p for a decimal point, replaced the p with a . and then converted to float. For example:

import re

def extract_num(s):
    return float(re.search(r'\d+p?\d*', s).group().replace('p', '.'))

strs = ['ms2p5', 'ms3', 'ms10']
print([extract_num(s) for s in strs])

Output:

[2.5, 3.0, 10.0]

Use str.join with re.findall :

los = ['ms2p5', 'ms3', 'ms10']
print([float('.'.join(re.findall('\d+', i))) for i in los])

Output:

[2.5, 3.0, 10.0]

If the strings all follow the examples you provide, I'd probably just do:

x = 'ms2p5'
float(x[2:].replace('p', '.'))

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