Question

I want to add spaces after and before comma's in a string only if the following character isn't a number (9-0). I tried the following code:

newLine = re.sub(r'([,]+[^0-9])', r' \1 ', newLine)

But it seems like the \1 is taking the 2 matching characters and not just the comma. Example:

>>> newLine = "abc,abc"
>>> newLine = re.sub(r'([,]+[^0-9])', r' \1 ', newLine)
"abc ,a bc"

Expected Output:

"abc , abc"

How can I tell the sub to take only the 'comma' ?

Was it helpful?

Solution

Use this one:

newLine = re.sub(r'[,]+(?![0-9])', r' , ', newLine)

Here using negative lookahead (?![0-9]) it is checking that the comma(s) are not followed by a digit.

Your regex didn't work because you picked the comma and the next character(using ([,]+[^0-9])) in a group and placed space on both sides.

UPDATE: If it is not only comma and other things as well, then place them inside the character class [] and capture them in group \1 using ()

newLine = re.sub(r'([,/\\]+)(?![0-9])', r' \1 ', newLine)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top