I need to replace all commas except those between numbers (ignoreing spaces). Some examples are:
foo, bar 12,6 => foo bar 12,6
foo, bar 12, 6 => foo bar 12, 6
foo, bar 12 ,6 => foo bar 12 ,6
foo, bar 12, => foo bar 12
foo, bar ,6 => foo bar 6
foo,5 => foo5
foo ,5 => foo 5
logic:
- do not replace if number before and after
- replace if number before
- replace if number after
- replace if no surrounding numbers
I've tried with a negarive lookahead and negative lookbehind:
# regex:
# (?<!\d)(\s?)(,)(\s?)(?!\d)
import re
def replace_comma_by_space(value):
return re.sub(r'(?<!\d)(\s?)(,)(\s?)(?!\d)', r'\1\3', value)
but this fails on cases like:
foo,5 => foo,5
foo ,5 => foo ,5
I feel like im close, thanks in advance for any help.