Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Need to find name of title in my pages. Here is where I am searching from:
<META NAME="TITLE" CONTENT="Page Title Here">
This is my attempt at it:
$line =~ /<meta name="title" content=\"(.+)>/i) print $1;

Replies are listed 'Best First'.
Re: Grabbing meta tag info
by gellyfish (Monsignor) on Jun 19, 2003 at 12:07 UTC

    Use the power of HTML::Parser :

    use HTML::Parser; my $p = HTML::Parser->new( api_version => 3, start_h => [\&start, "self,tagname, at +tr"] ); $p->parse('<META NAME="TITLE" CONTENT="Page Title Here">'); sub start { my ( $self, $tag, $attr ) = @_; if ($tag eq 'meta' && lc($attr->{name}) eq 'title' ) { print $attr->{content}; $self->handler(start => '' ); } }
    /J\
    

Re: Grabbing meta tag info
by Tomte (Priest) on Jun 19, 2003 at 12:08 UTC

    small fix:

    my $line = '<META NAME="TITLE" CONTENT="Page Title Here">'; $line =~ /<meta name="title" content="([^"]+)">/i; print $1; __END__ Page Title Here

    If you do more html-processing than that, consider to use a readymade module instead of regexp'ing your way through it.

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      If you do more html-processing than that, consider to use a readymade module instead of regexp'ing your way through it.

      Yes, I can definitely agree with that, but if you do want to take your regex route, you might want to use a zero-width positive look-behind assertion like /(?<=<meta)content=([^"]+)/ to decrease the likelihood it will break if the tag attributes are not in the same order. :)

      --
      Allolex

Re: Grabbing meta tag info
by davorg (Chancellor) on Jun 19, 2003 at 12:16 UTC

    Parsing HTML using a regex is dangerous and error-prone. Use an HTML parser instead as recommened by gellyfish.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

•Re: Grabbing meta tag info
by merlyn (Sage) on Jun 19, 2003 at 19:43 UTC