Page 1 of 1

Sanity Check on a RE Replace

Posted: Fri Sep 06, 2013 9:52 pm
by kengrubb
I'm doing a RE Replace, and here are the results I get.

Scenario #1
Find what: \xFF
Replace with: \
Result: Replaces ÿ with \

Scenario #2
Find what: \xFF
Replace with: \\
Result: Replaces ÿ with \

Scenario #3
Find what: \xFF
Replace with: \\\\
Result: Replaces ÿ with \\

The Replace with value would seem to resolved from RE into a literal if it's a valid RE (Scenarios #2 and #3), but otherwise it's treated as a literal (Scenario #1).

I am guessing it has always been that way, but should it?

Posted: Sat Sep 07, 2013 10:05 am
by ben_josephs
When regular expression is selected, certain characters in the replacement expression (the "replacement format string") have special meanings.

TextPad's help, under Reference Information | Replacement Format Strings, states:
These format strings treat all characters as literals except for '$', '\', '(', ')', '?', and ':'. To output any of those characters as a literal, precede it with a '\'.
It would appear that, as a convenience to the user, if a backslash stands alone in a replacemnet expression (or is unpaired at the end of a replacemnet expression), so that it plainly is not quoting anything, it's treated as a literal. The alternative would be to treat it as an error and annoy the user.

Note that a replacement format string is not a regular expression.