in reply to =~ and substitutions

Hello, This is how to get the code between the tags
$_ =~ /(<p>)(.*)(<\/p>)/i; print $2;
The number $2 picks up the value since it is in the second set of ().
hope this helps

Replies are listed 'Best First'.
Re: Re: =~ and substitutions
by chromatic (Archbishop) on Mar 03, 2003 at 20:28 UTC

    Careful! There are a few gotchas in that snippet. First, there's a greedy match. If you have a string containing <p>one paragraph</p><p>another paragraph</p>, $2 will contain one paragraph</p><p>another paragraph. Besides that, if the regex succeeds, $2 won't contain what you expect. You can also leave off the default variable and certainly don't need to capture the paragraph tags. I'd prefer:

    print $1 if m!<p>(.*?)</p>!;

    Of course, if there's anything more complicated than bare paragraph tags, you're better off using an HTML parser module.