How to replace a comma

Jon Clements joncle at googlemail.com
Mon Dec 18 08:17:27 EST 2006


Lad wrote:

> In a text I need to
> add a blank(space) after a comma but only if there was no blank(space)
> after the comman
> If there was a blank(space), no change is made.
>
> I think it could be a task for regular expression but can not figure
> out the correct regular expression.
> Can anyone help, please?
> Thank you
> L.

Off the top of my head, something like re.sub(', *', ', ', 'a,
b,c,d,e, f'), meets your requirements (it also ensures the number of
spaces after the comma is one). However, you may need to refine the
rules depending on what you really want to achieve. For instance, what
happens with: a comma appearing before any text, consecutive commas (ie
,,,), or commas within quotes?

hth
Jon.




More information about the Python-list mailing list