I have a Perl script that uses the following substitution regex to replace double quotes with a backslash + double quote combo:
(Since the backslash is not showing up in the code sample below after posting, I'm going to use a vertical bar in place of where the real code has a backslash.)
$str =~ s/|"/||"/isg;
However, the editor gets confused by this and thinks everything after the second double quote is now a literal string and colors it in that defined color.
If I remove one of the backslashes prior to the second double quote, the remainder of the script's colors are displayed properly.
Is this something I can fix in a config or syntax file or is this an issue that requires a code change?
Thanks!
Escaped quotes not handled properly with syntax colors
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
-
- Posts: 2461
- Joined: Sun Mar 02, 2003 9:22 pm
I don't think there is any way you can fix this. Perl syntax is complex; the only way an editor can parse it perfectly is by running Perl's own parser.
TextPad treats the first backslash as escaping the first double quote, so it treats it as not starting a string. It treats the second backslash as quoting the third backslash, leaving the second double quote unescaped, so it treats it as starting a string.
But you don't need the first backslash: just leave it out. Then TextPad will treat the text between the two double quotes as a string, but the string won't spill into what follows. (Also, /s means "." matches any character, including newline, and /i means match case-insensitively. Both of these are irrelevant in this regex replacement, so you can leave them out.)
If you can't avoid odd numbers of unescaped double quotes, you can put an extra one in a comment at the end of the line:
(You can get backslashes into your posts by doubling them.)
TextPad treats the first backslash as escaping the first double quote, so it treats it as not starting a string. It treats the second backslash as quoting the third backslash, leaving the second double quote unescaped, so it treats it as starting a string.
But you don't need the first backslash: just leave it out. Then TextPad will treat the text between the two double quotes as a string, but the string won't spill into what follows. (Also, /s means "." matches any character, including newline, and /i means match case-insensitively. Both of these are irrelevant in this regex replacement, so you can leave them out.)
Code: Select all
$str =~ s/"/\\"/g ;
Code: Select all
$str =~ s/"//g ; # "
Thanks. My previous editor handled this with regard to syntax highlighting so I was hoping it was simply an issue that could be corrected. Unfortunately, that editor had other unrelated issues that made it unstable in recent versions, so here I am evaluating other editors...
Although removing the first backslash does resolve the issue in this case, that's only because the replacement pattern also happens to have a double quote in it. In a different instance I use a similar regex to *remove* the double quote character, so there isn't a second double quote in the replacement pattern to "reset" the syntax highlighting, and the rest of the code following the regex is remains incorrectly highlighted.
Yes, I can add a commented quote, but I'd really prefer not to have to write unnecessary code just to satisfy the editor.
Although removing the first backslash does resolve the issue in this case, that's only because the replacement pattern also happens to have a double quote in it. In a different instance I use a similar regex to *remove* the double quote character, so there isn't a second double quote in the replacement pattern to "reset" the syntax highlighting, and the rest of the code following the regex is remains incorrectly highlighted.
Yes, I can add a commented quote, but I'd really prefer not to have to write unnecessary code just to satisfy the editor.