Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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?
thanks

Comment on to get next line of pattern matched
Select or Download Code
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

        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

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

Re: to get next line of pattern matched
by 2teez (Priest) 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?

Re: to get next line of pattern matched
by choroba (Abbot) 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() ;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-09-22 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (180 votes), past polls