in reply to Smart Search and Replacement with RegEx

This works for me:

$line= <<END; 10 PRINT "<29>This is dim.<30>EThis is reversed.<28>This is bold.<27>" END my %fix= ( "<29>" => '@(-38)', "<28>" => '@(-40)', "<27>" => '@(-35)' ); $line =~ s{("?)(<\d+>)("?)}{ if( "<30>" eq $2 ) { "$1$2$3" } else { my $str= $1 ? "" : '";'; $str .= $fix{$2}; $str .= $3 ? "" : ';"'; $str; } }gex; print $line;
Though I wish I could use return. (:

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: Smart Search and Replacement with RegEx
by sfink (Deacon) on May 29, 2002 at 18:59 UTC
    Here's another approach:
    $line= <<END; 10 PRINT "<29>This is dim.<30>EThis is reversed.<28>This is bold.<27>" END my %fix= ( "<29>" => '@(-38)', "<28>" => '@(-40)', "<27>" => '@(-35)' ); my $pattern = join("|", map "(?:$_)", keys %fix); 1 while $line =~ s/("[^"]*) ($pattern) (.*?")/$1";$fix{$2};"$3/gx; $line =~ s/"";//g; $line =~ s/;""//g; print $line;
Re: (tye)Re: Smart Search and Replacement with RegEx
by shelob101 (Sexton) on May 31, 2002 at 02:36 UTC
    hey, thanks tye. I knew it would take mere seconds for the wise ones here to enlighten me. One thing I seriously underuse in my perl programming is the extended capabilities of the replace clause of the s/// (or in this case, s{}{}) operator. Solution works perfectly for me!