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

$s = ' <p>something something</p> <h2>blah blah blah</h2> <p>something something</p> '; $before = '<h2>([^<]+)</h2>'; $after = '"<h1>$1</h1>"'; $s =~ s/$before/$after/ee; print $s;
<p>something something</p> <h1>blah blah blah</h1> <p>something something</p> Tool completed successfully

Replies are listed 'Best First'.
Re: Re: Supplying the RHS to a regex as a variable
by Cody Pendant (Prior) on Oct 18, 2003 at 07:19 UTC
    Picture me slapping my forehead. Thank you so much.

    I'd forgotten all about "ee" but now I'm remembering Merlyn's classic /old macdonald/eieio regex.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
Re: Re: Supplying the RHS to a regex as a variable
by pg (Canon) on Oct 18, 2003 at 06:19 UTC
    I can easily break this code by feeding it a valid html string:
    $s = ' <p>something something</p> <h2>blah <<< blah blah</h2> <p>something something</p> ';

    Update:

    BUU, although parsing is not the purpose, you have to realize that implicitly it is done any way. Regexp is obviously a kind of parsing, and it has been repeatedly mentioned by many monks here that, regexp is not a good way to deal with html.

    It is not an easy task to come up with perfect regexp for dealing with html.

    On the other hand, although the purpose is not parsing, parsing is still a valid tool, isn't it? In this case, actually a better tool.

      But he's not parsing html. The parent node never even mentioned parsing html. All he wants to do is match a certain substring and replace it with another one. The minor fact that this string happens to contain data that superficially resembles html has absolutely no bearing on this.