Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

regular expression

by ansh batra (Friar)
on Jan 07, 2013 at 09:06 UTC ( #1011981=perlquestion: print w/replies, xml ) Need Help??
ansh batra has asked for the wisdom of the Perl Monks concerning the following question:

my data is

</div><div class='ClipPicture'><a href=' +boardKeys/Cart/Acer/A_Series/A110/A8A'><img alt='Clip Style Pic' src= +'' width='659 +' height='135'/></a></div><div class='RadioButtonContainerClipStyle'>
this is one line of input
i want to get value of src attribute of img tag. i.e
my code
if($line=~ /<img .* src=\'(.*?)\' .*\/><\/a>/) { print "got2\n"; $kb_lay_img_url=$&; }

please help

Replies are listed 'Best First'.
Re: regular expression
by frozenwithjoy (Priest) on Jan 07, 2013 at 09:38 UTC
    You almost have it right. If I change $kb_lay_img_url=$&; to $kb_lay_img_url=$1;, it works for me. (see: Special Variables)

    EDIT: Here, I've made this change (and added Anon's suggestion, which makes the regex safer/better):

    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $line = "</div><div class='ClipPicture'><a href=' +yboardKeys/Cart/Acer/A_Series/A110/A8A'><img alt='Clip Style Pic' src +='' width='65 +9' height='135'/></a></div><div class='RadioButtonContainerClipStyle' +>"; if ($line=~ /<img .* src='([^']+)'.*\/><\/a>/) { print "got2\n"; my $kb_lay_img_url = $1; say $kb_lay_img_url; } __END__ got2
      thanks. :)
Re: regular expression
by choroba (Bishop) on Jan 07, 2013 at 09:36 UTC
    Do not use regular expressions to parse HTML. Use a proper tool. For example, using XML::XSH2, you can load the whole file and find the src attribute easilly:
    open :F html 1011981.html ; echo //img[@alt="Clip Style Pic"]/@src ;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: regular expression (blah blah blah)
by Anonymous Monk on Jan 07, 2013 at 09:07 UTC

      it is fetching the whole img tag
      i need only value of src attribute

        Most likely what Anonymous Monk showed you was not supposed to be the complete regular expression but intended to be used for matching the src attribute only. You will have to apply that hint appropriately.

        Did you try to understand my regex pattern? YAPE::Regex::Explain? Put it in your regex, match up the quotes, delete your stuff, use my stuff

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011981]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2018-05-23 01:48 GMT
Find Nodes?
    Voting Booth?