The opening curly bracket “{” is itself a special character in Perl. If you’re matching it in your regexp, you’ll also need to escape it with a backslash:
Because the backslash must be escaped twice - once in the assignment to $find, and once in the s/// operator - your code requires that you encode the backslash to be matched as four backslashes.
The following works:
my $find = "\{\\\\em";
$replace = "<em>";
$infile =~ s/$find/$replace/g;
If you don’t need variable interpolation it can be written as
tschild , that worked perfectly. It also makes sense now that you would need to escape it twice (though I never would’ve though of that). Thank you so much!
Terminus , just FYI, it appears to work for me BOTH ways (if I escape the “{”, but also if I don’t).
Ah the mysteries of Perl variable interpolation. When you assign a string to a scalar using double quotes:
$find = "{\\em"
Perl interprets the double backslash as a single backslash before it reaches the regex parser. So the regex parser only sees a single backslash, then inteprets the “\e” as an ASCII ESC character. To get around this, you need to backslash-escape both backslashes, or you can use single quotes, or use the search string in s/// directly. The latter is a good idea in any case because putting a variable in s/// forces the compiler to do extra work.
Ref: Programming Perl, 3rd ed. pp. 191-193
crozell: You’ve solved your problem, but I’m curious (and too lazy to do it myself). What happens if you say:
It’s because the OP was putting the text to be replaced in a variable. He wanted to replace {\em. When that’s placed in the replace command, the backslash needed to be escaped, so {\em. Now, if you use a variable to send that information, the variable needs to contain {\em. But to put that into the variable, the backslahes each need to be escaped again, hence {\\em.
Terminus , I tried it with the single quotes and it didn’t seem to work. Sorry!
It may seem (from the OP) that I am doing extra work by putting the search strings in a variable, but I have several strings I’m trying to replace in the same file. There may be better ways to do this, but I’m a novice at Perl and don’t use it often enough to put a lot of work learning it completely. If there is obviously a better way to do this and I’m just missing it, please let me know!
Ach, you probably need to backslash the curly brace if you use single quotes.
crozell: Looks like you’ve got a legitimate use for variable interpolation in s///. Your script sounds like a one-off deal (or is rarely run). So if it works for you, don’t sweat it!