in reply to Smart Search and Replacement with RegEx
$line = qq{10 PRINT "<29>This should be dim.<30>EThis should be revers +ed.<28>This is bold."}; my %fix = ( "<29>" => '";@(-38);"', "<28>" => '";@(-40);"', "<27>" => '";@(-35);"', "<30>" => "<30>", ); $line =~ s/"([^"]*)"/my $s = $1;$s =~ s{(<\d+>)}{$fix{$1}}g;'"'.$s.'"' +/ge; #or # $line =~ s/("[^"]*")/my $s = $1;$s =~ s{(<\d+>)}{$fix{$1}}g;$s/ge;
This is a regexp inside regexp. The outer one "extracts" all strings from a line, the second replaces the <\d+> thingies inside. This way it's safe even in the unlikely case that a <\d+> appears outside a string in the code.
Another thingie is that I keep the whole replacements in the hash, including the double quotes and semicolons, and replace <30> by itself. And an unknown <\d+> by nothing.
If you wanted to leave unknown alone you'd change the regregexpexp to
or$line =~ s/"([^"]*)"/my $s = $1;$s =~ s{(<\d+>)}{$fix{$1}||$1}ge;'"'.$ +s.'"'/ge;
Then you would not have to include the <30> in the %fix hash.$line =~ s/("[^"]*")/my $s = $1;$s =~ s{(<\d+>)}{$fix{$1}||$1}ge;$s/ge +;
I assume that to include a doublequote in a string (in that version of Basic) you have to double it.
Jenda
|
|---|