I am modifying some HTML pages and want to increase the font size dynamically with a regex. In my script below, I want the ‘8’ and ‘3’ to turn into ‘9’ and ‘4’ but I get ‘8++’ and ‘3++’, respectively. I have the following:
#!/usr/bin/perl
use warnings;
use LWP::Simple;
my $content = "<TD><FONT STYLE=\"font-family:Verdana, Geneva, sans-serif\" SIZE=\"8\">this is just a bunch of text</FONT></TD>";
$content .= "<TD><FONT STYLE=\"font-family:Verdana, Geneva, sans-serif\" SIZE=\"3\">more text</FONT></TD>";
$content=~s/SIZE="(\d+)">/SIZE="$1++">/g;
print $content;
I’ll just skip the part about how regexps are a bad way to parse HTML, because sometimes a quick-and-dirty solution is good enough.
You can’t use an operator inside a string like that. The ++ is just treated as plain text (as you found). You have to use the
/eflag to indicate that the replacement should be evaluated as Perl code, and then use the appropriate expression, like:You can’t use
$1++for two reasons. First, it would do the increment after returning the value, so you’d be replacing 8 with 8 instead of 9. Second,$1is a read-only value, and the increment would want to modify it.