I am trying to write a regex for python that replaces a dot, space or newline (and any combination of these) with a single comma. I don't understand why my regex is not working.
newline = line.replace("[\. \\n]+",",")
I am trying to write a regex for python that replaces a dot, space or newline (and any combination of these) with a single comma. I don't understand why my regex is not working.
newline = line.replace("[\. \\n]+",",")
You need to use sub to be able to use regex in search replace.
# your code goes here
import re
line = "something with space . dot";
line = re.sub(r'[. \n]+', ",", line);
print line;
The characters in between the square brackets ("character class") are literals so don't need escaping. Try [. \n]+
instead.
Edit
According to this answer Python string.replace regular expression replace
does not recognise regex and you need to use sub