2

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.

martineau
  • 112,593
  • 23
  • 157
  • 280
cheshire
  • 1,088
  • 1
  • 12
  • 30

2 Answers2

3

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
Dani Mesejo
  • 55,057
  • 6
  • 42
  • 65
3

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.

timgeb
  • 73,231
  • 20
  • 109
  • 138