[erlang-questions] Why is it necessary to "double-escape" [ characters in regular expressions?

Jachym Holecek freza@REDACTED
Mon Mar 30 09:21:18 CEST 2009

# David Mitchell 2009-03-30:
> However, "double-escaping" the [ characters (by adding a second \ prior to
> the [ character) does exactly what I want:
>   re:replace("abc123 <![CDATA[< abc123", "<!\\[CDATA\\[<", "<", [{return,
> list}])
> returns "abc123 < abc123", which is the result I'm after.

Backslash is an escape character within string syntax, like in C.
So if you want your literal string to contain a backslash (you
do, in order to remove the special meaning '[' has in REs),
you need to write "\\" as you discovered. See section 2.14 of
Erlang Reference Manual for more details.

	-- Jachym

More information about the erlang-questions mailing list