简体   繁体   English

python regex - 从带有数字和字符的字符串中提取数字

[英]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'、'ms3'、'ms10' 之类的字符串,我需要提取数字以转换为数字,如下所示。

'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'.一个问题是在提取的字符串中间有一个字符的值,如“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:您可以编写一个搜索数值的提取函数(带或不带p表示小数点,用.替换p ,然后转换为浮点数。例如:

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 :str.joinre.findall str.join使用:

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', '.'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM