4 ms·
> The regex here is using an r"" string, as all regexes should. I don't understand how \ ever became the escape character for regular expressions (and JSON!) i
by thaumasiotes 11y ago
> The regex here is using an r"" string, as all regexes should.
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:
re.compile('++') # matches escaped + in the data
re.compile('@@') # matches @ in the data
re.compile('\\') # matches \ in the data
- eridius 11y agoWhy are you using regexes to search through raw JSON? That's pretty messy. 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.
- thaumasiotes 11y ago> Why are you using regexes to search through raw JSON? >> (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.
- eridius 11y agoSince you had to pick a contrived example, that suggests the problem isn't particularly common. If it was "real" enough, you wouldn't need a contrived example. > 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.
- eru 11y agoInstead of escaping, we could also just prefix all our strings with their length. (Editor support appreciated.)
- thaumasiotes 11y agoNo, that fails to address nearly all the use cases of escaping. Escaping is the difference between printf("\n") and printf("n"), or between printf("d") and printf("%d"), or between (dog|cat)* (any number of repetitions of "dog" and "cat") and \(dog|cat\)* (a left paren, exactly one instance of "dog" or "cat", and any number of right parens). Those aren't differences of length. The only scenario marked length helps with is situations like "I need to use \"scare quotes\"".
- thaumasiotes 11y agoActually, I feel compelled to note that as written, \(dog|cat\)* doesn't do what I said; it matches stuff like this: (dog cat)))) cat What I said embarrassingly requires another set of non-escaped parens: \((dog|cat)\)* :(
- eru 11y agoGood point!