从字符串中选择数字和满分

Choosing numbers and full points from a string

我写了下面的代码来从字符串中选择数字。我只得到整数,我也想得到小数。

def vreplace_chars(third): 
                vlist_of_numbers = re.findall(r'\d+',third)
                vresult_number = '---'.join(vlist_of_numbers)
                return vresult_number
            number=vreplace_chars(imgchar)

更改正则表达式以选择性地包含小数部分 \d+(?:.\d+)?

你需要改变你的模式。也许类似的东西应该对你有帮助。例如,这里的小数点可以用“,”或“.”来捕获。 :

import re
def vreplace_chars(third, pattern= r"(\d+(\,|.\d+)*)"):
        vresult_number = '---'.join([x[0] for x in re.findall(pattern, third)])
        return vresult_number
  
>>> vreplace_chars("okok9.0,5689.0")
'9.0,---5689.0'

如果要查找带“.”的小数点只有,定义模式如下:

pattern= r"(\d+(.\d+)*)"