in reply to Re^2: Regular Expression Doubt
in thread Regular Expression Doubt

my $text = '1 &plus; 2 <br> <maths> &plus; dfdf</maths>'; my $output = ""; while ($text =~ m/((<maths>.*?<\/maths>)|([^<]*))/gs) { if ($2) { $output .= $2; } else { my $segment = $3; $segment =~ s/&plus;/&thinsp;&plus;&thinsp;/g; $output .= $segment; } } print $output . "\n"; __END__ 1 &thinsp;&plus;&thinsp; 2 br> <maths> &plus; dfdf</maths>
You lost a < here.

Replies are listed 'Best First'.
Re^4: Regular Expression Doubt
by reasonablekeith (Deacon) on Apr 05, 2005 at 15:09 UTC
    dammit
    my $output = ""; while ($text =~ m/((<maths>.*?<\/maths>)|&plus;|.)/gs) { if ($2) { $output .= $1; } else { my $segment = $1; $segment =~ s/&plus;/&thinsp;&plus;&thinsp;/g; $output .= $segment; } } print $output . "\n";
      Eh, if you go that way, it could be made much more efficient. No need for the s/// - you're matching any &plus; already:
      my $output = ""; while ($text =~ m{\G(?:&plus;|(<maths>.*?<\/maths>|[^&<]+|.))}gs) { $output .= $1 || "&thinsp;&plus;&thinsp;"; } print $output, "\n";