Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regex to add space after dot or comma

Tags:

python

regex

I have a string as follows:

line = "This is a text.This is another text,it has no space after the comma."

I want to add a space after dots and commas, so that the end result is:

newline = "This is a text. This is another text, it has no space after the comma."

I tried the solution from here: Python Regex that adds space after dot, but it does work only for dots or commas. I have not been able to grasp how to get the regex recognize both characters at once.

like image 693
maurobio Avatar asked May 30 '17 13:05

maurobio


1 Answers

Use this regex to match locations where preceding character is a dot or a comma and the next character isn't a space:

(?<=[.,])(?=[^\s])
  • (?<=[.,]) positive lookbehind that looks for dots or commas
  • (?=[^\s]) positive lookahead that matches anything that isn't a space

So this will match positions just after the comma or the space like ext.This or text,it. but not word. This.

Replace with a single space ()

Regex101 Demo

Python:

line = "This is a text.This is another text,it has no space after the comma."
re.sub(r'(?<=[.,])(?=[^\s])', r' ', line)

// Output: 'This is a text. This is another text, it has no space after the comma.'
like image 113
degant Avatar answered Sep 28 '22 07:09

degant