Reputation: 1159
My regex
\d+\.*\d+
my String
4a 1 a2 3 21 12a3 123.12
What I get:
['21', '12', '123.12']
What I need:
412321123123.12
I do work well when there is just 123.12 for example but when I add spaces or new chars in between it separates them. I want it to skip all the spaces and chars and to extract just numbers separated with '.' in the right position.
Upvotes: 2
Views: 111
Reputation: 78690
Regex-free alternative:
>>> s = '4a 1 a2 3 21 12a3 123.12'
>>>
>>> ''.join(c for c in s if c.isdigit() or c == '.')
>>> '412321123123.12'
Using a list comprehension with str.join
is slightly faster than the generator-comprehension, i.e.:
>>> ''.join([c for c in s if c.isdigit() or c == '.'])
>>> '412321123123.12'
This assumes that you want to keep multiple .
or that you don't expect more than one.
Upvotes: 3
Reputation: 61910
Remove everything that is not a digit or a point:
import re
result = re.sub('[^.\d]', '', '4a 1 a2 3 21 12a3 123.12')
print(result)
Output
412321123123.12
Upvotes: 3