[erlang-questions] Why is it necessary to "double-escape" [ characters in regular expressions?
Tony Finch
dot@REDACTED
Mon Mar 30 17:06:49 CEST 2009
On Mon, 30 Mar 2009, David Mitchell wrote:
>
> 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.
There are two levels of backslash escaping here, one for string literals
and one for regular expressions. The \\ in the string literal becomes \ in
the run-time string, which the regex implementation treats as an escape
character for the following [. If you only write \[ in the string literal
then this becomes [ in the run-time string which the regex implementation
treats as the start of a character class specifier, and since there's no
closing ] it throws a syntax error.
Tony.
--
f.anthony.n.finch <dot@REDACTED> http://dotat.at/
GERMAN BIGHT HUMBER: SOUTHWEST 5 TO 7. MODERATE OR ROUGH. SQUALLY SHOWERS.
MODERATE OR GOOD.
More information about the erlang-questions
mailing list