Home > Software design >  Regex add a space between words that ends before the letter S with succeeding numbers or before the
Regex add a space between words that ends before the letter S with succeeding numbers or before the

Time:11-05

I am trying to achieve this with re.sub.

Input1: RICHARDJAMESS1234567A

Output1: RICHARDJAMES S1234567A

Input2: ALFAROMEO<P>

Output2: ALFAROMEO <P>

I have tried this code but it isn't working.

re.sub(r'([a-zA-Z])([Ss] (\d{7}|<)$', r'\1 ', report)

what are the changes I should be making in order to make this work?

CodePudding user response:

You could find the start index of the marker and split your string there:

import re

def separate(text):
    match = re.search(r'(S\d |<)', text)
    if match:
        idx = match.start()
        return text[:idx]   ' '   text[idx:]
    return text

print(separate('RICHARDJAMESS1234567A'))
print(separate('ALFAROMEO<P>'))

gives

RICHARDJAMES S1234567A
ALFAROMEO <P>

CodePudding user response:

You can use

text = re.sub(r'S\d|<', r' \g<0>', text)

See the regex demo.

The S\d|< matches S and a digit or a < char, and the whole match is replaced with a space itself.

See the Python demo:

import re
text = "RICHARDJAMESS1234567A ALFAROMEO<P>"
print( re.sub(r"S\d|<", r" \g<0>", text) )
# => RICHARDJAMES S1234567A ALFAROMEO <P>
  • Related