Home > front end >  Replace incorrect occurrence of comas
Replace incorrect occurrence of comas

Time:01-24

I would need to replace all incorrect occurrences of coma in the text

jlkj,jlkj
jlkj, jlkj
jlkj,jlkj
jlkj , jlkj

with , , that means it would be jlkj, jlkj in result for all the cases.

The the pattern I came up with does not work: (.*),(.*)

CodePudding user response:

Use

re.sub(r'\s*,\s*', ', ', your_string)

See regex proof.

REGEX101 EXPLANATION

\s* -  matches any whitespace character (equivalent to [\r\n\t\f\v  ])
       between zero and unlimited times, as many times as possible, giving 
       back as needed (greedy)
,   -  matches the character , with index 4410 (2C16 or 548) literally
\s* -  matches any whitespace character (equivalent to [\r\n\t\f\v  ])
       between zero and unlimited times, as many times as possible, giving 
       back as needed (greedy)
  • Related