The short answer for "why does everything use \" is because using a different escape character for each different grammar very quickly exhausts the potential space of uncommon punctuation, and becomes exceedingly confusing as anyone who works with more than one of these grammars now has to remember which one uses which escape character.
>> (because this is an example to show why sharing escape characters between languages is idiotic)
I turned one backslash into eight backslashes with a contrived example. While it's not common to go through so many layers, this problem is very real, and, in the article, is directly responsible for a bug.
> using a different escape character for each different grammar [...] becomes exceedingly confusing as anyone who works with more than one of these grammars now has to remember which one uses which escape character
Are you really arguing that we could make C less confusing by switching printf("%d", x) to printf("\\d", x)? I'd say that C strings and C format directives use different escape characters specifically because they're likely to be used together. But regexes are intended to be used with strings, too.
> and, in the article, is directly responsible for a bug.
Nope! It was not responsible for anything whatsoever. Yeah, the backslashes got escaped, but they were already broken, so it didn't actually break anything. All it did was make "btest" match instead of "\x08test" match (both of which are equally buggy, though "btest" is more likely to be triggered in the wild).
> Are you really arguing that we could make C less confusing by switching printf("%d", x) to printf("\\d", x)?
No. I did not say that at all, and I don't appreciate straw man arguments.
> I'd say that C strings and C format directives use different escape characters specifically because they're likely to be used together
I doubt it. Neither of us was present (I'm assuming) when the printf format spec was decided upon, but I'd wager that % was chosen because this has a semantically different meaning than backslash-escapes. Backslash escapes are just that: escapes. They're either stand-ins for certain un-typeable characters, or they disable interpretation of the following character. But % in printf isn't used as a character escape, it's used as a token. It's a stand-in for a more complicated representation of a separate argument. And the fact that it behaves differently than \ does warrants it using a different character.
Whereas in other languages, \ typically has the exact same semantic meaning as \ does in C strings (the set of character escapes may vary slightly, although most languages tend to copy C's character escapes verbatim). And that's why they use \. It's the same reason most languages use "" to denote strings, or [] to denote subscripting. It's common punctuation that behaves the same across multiple languages, which makes it easy for the programmer to remember and work with.
>> Are you really arguing that we could make C less confusing by switching printf("%d", x) to printf("\\d", x)?
> No. I did not say that at all, and I don't appreciate straw man arguments.
This is an argument in a very standard form, reduction to absurdity.
I see printf escapes and C string backslash-based escapes as semantically identical; they are both ways of giving the parser instructions directly rather than giving it a literal representation of what you want (this analysis also nicely explains why they're called "escapes"). But since there is a good amount of evidence that a lot of other programmers feel the same way you do, let me ask this: HTML escapes (with &) have exactly the semantics you attribute to \. Do you think HTML would be better if it used backslashes instead?
(dog
cat))))
cat
What I said embarrassingly requires another set of non-escaped parens: \((dog|cat)\)*:(
I don't understand how \ ever became the escape character for regular expressions (and JSON!) in the first place. Every language gets its own unique escape character -- that way, you don't need an exponential number of characters in the source code to represent a single character in the model. This seems to have been understood early on; C uses \ as the escape character for strings and % as the escape character for printf directives. Common Lisp uses \ for strings and ~ for format directives. HTML uses &. When I wrote a regex parser, I made the escape character /, making a clear distinction between \n (a raw newline - just character data to the regex parser) and /n (an escaped n - special to the regex parser). But somehow every mainstream regex library uses \, gratuitously adding a lot of mental load to figuring out how much escaping you need. String.split("\\s+") is a usability failure, not something to be proud of, and definitely not something to imitate. What happened?
Imagine you're looking for use of a single backslash in some JSON data. It'll be escaped, appearing literally in the data as \\. If (because this is an example to show why sharing escape characters between languages is idiotic) you want to search for it with a regex, you need \\\\. But to get that regex, you need to escape each of those four backslashes yet again, because you construct them from strings. So you've got re.compile('\\\\\\\\') to match what is notionally a single backslash in the data you're searching through. Compare a hypothetical world where \ is the escape character for strings, @ is the escape character for regexes, and + is the escape character for JSON: