[erlang-questions] Backslashing in re:replace

Attila Rajmund Nohl attila.r.nohl@REDACTED
Tue May 12 12:26:21 CEST 2009


Hello!

I'd like to replace all dot characters in a string with backslash-dot
sequences. My first try was

re:replace("f.e.l.", "\\.", "\\.", [{return, list}, global])

Because Erlang "eats up" the backslash characters, the actual regular
expression in the second argument is "\." which is exactly what I
want. For the same reason the actual replacement string is also "\."
which is also what I want. However, the result is not what I expected:

> re:replace("f.e.l.", "\\.", "\\.", [{return, list}, global]).
"f.e.l."

When I added a couple of more backslashes to the replacement string, I
got what I wanted:

> re:replace("f.e.l.", "\\.", "\\\\.", [{return, list}, global]).
"f\\.e\\.l\\."

but I don't understand why I need four backslashes instead of two. Any
light on this issue?



More information about the erlang-questions mailing list