Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

to get next line of pattern matched

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

the data i have is

<div class='PlayVideo'></div> A8A <div class='RadioButtonClipStyle'>

what is am doing is
foreach $l(@lines) {#traversing the file if($line=~ /<div class=\'PlayVideo\'><\/div>\n(.*?)/) { $kb_layout_id=$&; print "$kb_layout_id\n"; } }

i want to fetch A8A. can you guyz tell me where i am going wrong?

Replies are listed 'Best First'.
Re: to get next line of pattern matched
by choroba (Bishop) on Jan 07, 2013 at 09:39 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 extract the string easily:
    open :F html 1011977.html ; echo //div[@class="PlayVideo"]/following-sibling::text() ;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: to get next line of pattern matched
by Anonymous Monk on Jan 07, 2013 at 09:04 UTC

    You're not using quotemeta :)

     my( $lid ) = m{\Q<div class='PlayVideo'></div>\E(.*?)<div class='RadioButtonClipStyle'>}s;

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

        strike it out and update with an explanation why, rather than removing the title and post conents.

        No it isn't, there is no image tags in the data you posted

        If you can't keep your posting straight, take a break

Re: to get next line of pattern matched
by 2teez (Vicar) on Jan 07, 2013 at 09:25 UTC

    In a much as I will not recommend using regex to parse HTML doc., however, for this you could use Look-Around Assertions like so:

    use warnings; use strict; my $str = "<div class='PlayVideo'></div> A8A <div class='RadioButtonClipStyle'>"; if ( $str =~ m#(?<=</div>)(.+?)(?=<div.+?>)#s ) { print $1; ## prints A8A }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      your solution is correct but the file i am parsing contains many div tag
      can you please provide the more specific solution

        Since you're having so many problems achieving this with a regex, have you considered taking the previous advice of using a HTML Parser?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011977]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-10-18 20:36 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (105 votes). Check out past polls.