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
  • 119,623
  • 25
  • 170
  • 301
cheshire
  • 1,109
  • 3
  • 15
  • 37

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
  • 61,499
  • 6
  • 49
  • 76
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
  • 76,762
  • 20
  • 123
  • 145