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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With