> On Oct 28, 2023, at 6:29 AM, Kaveh Bazargan <[email protected]> wrote:
> 
> But I believe that in other environments, e.g. other programming languages, 
> you sometimes need to escape. I think sometime with \" and sometimes ""
> 

The problem is that 

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

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "[email protected]" rather than 
posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/2FED3052-5F34-48B3-BA87-648A196B61BB%40faiman.org.

Reply via email to