user3206440
user3206440

Reputation: 5059

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() 

Upvotes: 1

Views: 673

Answers (3)

Haleemur Ali
Haleemur Ali

Reputation: 28273

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

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

Upvotes: 1

Nick
Nick

Reputation: 147196

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]

Upvotes: 2

U13-Forward
U13-Forward

Reputation: 71580

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]

Upvotes: 2

Related Questions