- Regular expressions themselves are a moderately complex language with a specific syntax, but
- Regular expressions are often used in other languages, where they may also be subject to the syntax rules of those languages.
The only things that need to be “escaped” in the regular expression language are the regular expression special marker characters: parentheses, dots, plus signs, asterisks, question marks, brackets, backslashes, some letters …
When you use a regular expression in BBEdit, in a Find and Replace dialog or one of the special Text menu operations, you are just writing a regular expression, so only the regular expression language rules apply, and the only things that need to be escaped are the regular expression operator characters. (Single backslashes before other characters generally are just ignored, like the backslashes before the quote marks in this example; but best practice is probably to use only the backslashes that are required by the regular expression syntax.)
But, for example, when you use a regular expression in the Perl language, the regular expression (often) has slashes around it to show that it is a regular expression, so if there are slashes in the regular expression, they need to be escaped.
In some languages, a regular expression is just written as a a string literal, which means that it has to satisfy the language rules for a string literal. In particular, backslashes are special in string literals in the C-family languages, which means that any backslashes in a string literal have to be escaped, as well as any quote marks, which otherwise would mark the end of the string.
Thus, the regular expression (.+"ERROR".+)\r(.+)\r(.+)\r(.+) as a string literal in C would be "(.+\”ERROR\".+)\\r(.+)\\r(.+)\\r(.+)” where the red backslashes are needed so that the quotes around ERROR won’t look like the end of the string, and so that the character sequence \r is part of the regular expression (otherwise, the \r would be transformed into a carriage-return character by the C string literal parser, and the regular expression would contain return characters instead of backslash-r sequences).
Regards,
Neil Faiman