in reply to Supplying the RHS to a regex as a variable

A better way is to use existing modules to parse html for you, for example HTML::Parser: (code is only used to demo the idea. Obviously you need to add more handling, but parsing is done for you correctly.)
use HTML::Parser; use strict; my $str = '<p>something something</p> <h2>blah <<< blah blah</h2> <p>something something</p>'; my $parser = HTML::Parser->new(default_h=> [\&handler, "tagname, text" +]); $parser->parse($str); sub handler { my ($tag, $text) = @_; if ($tag) { if ($tag eq "h2") { print "<h1>"; } else { print "<$tag>"; } } else { print $text; } }

Replies are listed 'Best First'.
Re: Re: Supplying the RHS to a regex as a variable
by BUU (Prior) on Oct 18, 2003 at 07:19 UTC
    I can easily break this code by feeding it:
    my $str = '<p>something something</p> <h2>blah </a> blah blah</h2> <p>something something</p>';

    Output:
    <p>something something<p> <h1>blah <a> blah blah<h1> <p>something something<p>
    (Whered the slash before the a go? Heck if I know)
Re: Re: Supplying the RHS to a regex as a variable
by Cody Pendant (Prior) on Oct 18, 2003 at 07:43 UTC
    Thanks for the code. It really was just an example though. I was thinking about the Big Picture rather than the code at hand.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print