Add Space After Commas Only If It Doesn't Already

Add space after commas only if it doesn't already?

Using negative lookahead to check no space after comma, then replace with comma and space.

print 'word word,word,word,'.gsub(/,(?![ ])/, ', ')

Add a space after commas in a string only if it doesn't exist in R

We can use gsub to match a comma (,) followed by any letter (([A-Za-z])) captured as a group and then replace it with , followed by a space and the backreference of that captured group (\\1)

gsub(",([A-Za-z])", ", \\1", a)
#[1] "Jack and Jill, went up the, hill, to, fetch a pail, of, water"

Or use [[:alpha:]]

gsub(",([[:alpha:]])", ", \\1", a)
#[1] "Jack and Jill, went up the, hill, to, fetch a pail, of, water"

Python regex : adding space after comma only if not followed by a number

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)

Add missing spaces after commas

You can use capturing group:

%s/,\(\S\)/, \1/g

\(\S\) is being used to capture the next non-space character after a comma.

OR you can avoid the capturing using positive lookahead:

:%s/,\(\S\)\@=/, /g

Or to avoid the escaping using very magic:

:%s/\v,(\S)\@=/, /g

Add space after comma to string

Don't include arguments in your anonymous function since this is overwriting your $string value... As suggested by @RoryMcCrossan you don't need a function