[erlang-bugs] Module re, escape needs double backslash
Erik Søe Sørensen
ess@REDACTED
Tue Nov 20 11:24:06 CET 2012
Not a bug, just a surprise -
The string
"/\\*.*?\\*/"
does only contain two backslashes:
> length([C || C <- "/\\*.*?\\*/", C==92]). % The character code
for backslash being 92.
2
The string literal in the *Erlang source file*[1], of course, plainly
contains four of them.
The surprise is that there are two interpreters involved -- two layers,
each of which requires backslash-escaping:
1. The Erlang parser reads [Backslash, Backslash] and puts a single
backslash into the string literal (rather than taking the second
backslash as a signal to start an escape sequence).
2. The "re" module reads [Backslash, Asterisk] and interprets this by
taking the asterisk literally (rather than as a zero-or-more modifier).
Or going the other way: If we desire a literal asterisk in the pattern,
we must escape it, so that "re" sees a backslash in front of the asterisk.
Thus, we want a backslash in the string. And if we want to write that as
a string literal, then in order for the string to contain a backslash,
we must put another backslash in front of it in the Erlang source code,
because backslash means something special to the Erlang parser as well.
/Erik
[1] Or in this case, the expression typed into the Erlang shell.
On 20-11-2012 09:17, Arif Ishaq wrote:
> Hi,
> The backslash character as escape doesn't work as expected.
> Erlang R15B (erts-5.9) [smp:4:4] [async-threads:0]
> Eshell V5.9 (abort with ^G)
> 1> String = "/* this is a C comment */".
> "/* this is a C comment */"
> 2> re:run(String, "/\*.*?\*/").
> ** exception error: bad argument
> in function re:run/2
> called as re:run("/* this is a C comment */","/*.*?*/")
> 3> re:run(String, "/\\*.*?\\*/").
> {match,[{0,25}]}
> 4>
> Best regards
> PS. The documentation in "erl5.9/lib/stdlib-1.18/doc/html/re.html" says:
> ".. the pattern
> /\*.*?\*/
> does the right thing with the C comments."
--
Mobile: + 45 26 36 17 55 | Skype: eriksoesorensen | Twitter: @eriksoe
Trifork A/S | Margrethepladsen 4 | DK-8000 Aarhus C |
www.trifork.com <http://www.trifork.com/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-bugs/attachments/20121120/15b869d7/attachment.htm>
More information about the erlang-bugs
mailing list