Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping backslash In elisp

I am trying to create the string \] in elisp for inclusion in a regular expression. When I return the string \\] I get the string \\], but when I return the string \] I get the string ]. This doesn't really make any sense to me. Here is output from the ielm:

ELISP> "\\"
"\\"
ELISP> "\\]"
"\\]"
ELISP> "\]"
"]"

What is going on here. Why isn't the first backslash escaping the second one?

like image 579
imalison Avatar asked Feb 05 '13 06:02

imalison


1 Answers

What you're missing is that strings print the same way you have to type them. So if a string contains a character that requires escaping, a backslash will be printed before it. But if it contains a character that doesn't require escaping, there won't be one printed.

When you type "\\", it creates a string containing a single backslash character. This gets printed as "\\".

When you type "\\[", it creates a string containing two characters: backslash followed by square bracket. This gets printed as "\\[".

When you type "\[", the backslash escapes the square bracket. This escaping is redundant, since square brackets don't need to be escaped. So it creates the same string as "[": a string containing the single square bracket character. This gets printed as "[", because there's no need to escape square brackets.

like image 57
Barmar Avatar answered Nov 15 '22 11:11

Barmar