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
alternatively your can also be solved without regex as follow :
>>> line = "This is a text.This is another text,it has no space after the comma."
>>> line.replace('.', '. ', line.count('.')).replace(',', ', ', line.count(','))
'This is a text. This is another text, it has no space after the comma. '
>>>
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 spaceSo 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.'