Python: replacing a space after numbers keeping a space after letters

As part of the preprocessing of my data. I want to be able to replace the space followed by a number, keeping the space followed by the character. For instance:

Input String: '8.1.7 Sep 2000 Dec 2004 Dec 2006 Indefinite'

Expected output: '8.1.7,Sep 2000,Dec 2004,Dec 2006,Indefinite'

I am using an expression-based replacement function in python:

re.sub("\s+", ",", release) 

but this will not produce the desired result, simply because it means replacing all spaces, not sure how to save the one followed by the characters, i.e. [a-z].

Or maybe I need to rethink the approach.

+4
source share
2 answers

You can use (?<=\d)numbers before the space to query:

release = re.sub(r"(?<=\d)\s+", ",", release)

Watch the regex demo

More details

  • (?<=\d) - lookbehind, , .
  • \s+ - 1 .
+7

:

>>> re.sub(r"(\d)\s+", r"\1,", '8.1.7 Sep 2000 Dec 2004 Dec 2006 Indefinite')
'8.1.7,Sep 2000,Dec 2004,Dec 2006,Indefinite'

, :

release = re.sub(r"(\d)\s+", "\1,", release)

Explaination:

  • (\d)
  • \s+
  • : \1 , , ( )
+4

Source: https://habr.com/ru/post/1694229/


All Articles