I have the following string numeric values, and need to keep only the digit and decimals. I just can't find a right regular expression for this.
s = [
"12.45-280", # need to convert to 12.45280
"A10.4B2", # need to convert to 10.42
]
You can also remove all non-digits and non-dot characters, then convert the result to float:
In [1]: import re
In [2]: s = [
...: "12.45-280", # need to convert to 12.45280
...: "A10.4B2", # need to convert to 10.42
...: ]
In [3]: for item in s:
...: print(float(re.sub(r"[^0-9.]", "", item)))
...:
12.4528
10.42
Here [^0-9.] would match any character except a digit or a literal dot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With