perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this:

I have in my response lines like:

	<div class="sender">mimi020</div>
<some html code.....>
<div class="sender">jane01</div>
..and so on

so 1. search for every "sender" word
2. print
"mimi020"
"jane01"

how to do that? thank you for your help :slight_smile:

while (<>) {  # reads STDIN, put a different handle here if you want
  if (/<div class="sender">(.*)<\/div>$/) {
    print "\"$1\"\n";   # prints with " symbols
    print "$1\n";        # prints without " symbols
  }
}

As your html scraping needs evolve, you'll definitely want to follow the advice @
perlfaq6 - How do I match XML, HTML, or other nasty, ugly things with a regex?

If Smiling Dragon's suggestion works for you, keep in mind that it can only handle a single instance of that pattern per line.

Regards and welcome to the forum,
Alister

hi, thank you for your answer but I'm not sure how to use it,

is this ok?

while ($response2->content()) {  # reads STDIN, put a different handle here if you want
  if (/<div class="sender">(.*)<\/div>$/) {
    print "\"$1\"\n";   # prints with " symbols
    print "$1\n";        # prints without " symbols
  }
}

it gives me an (infinity?) errors

Use of uninitialized value $_ in pattern match (m//) at p2.pl line 103, <> line 1.

so what am I doing wrong?

---------- Post updated 08-05-12 at 10:46 AM ---------- Previous update was 08-04-12 at 02:37 PM ----------

ok I got it:

my $html = $response2->content;
  while( $html =~ m/<div class="sender">(.*)<\/div>/g) {   
 # print "\"$1\"\n"; # prints with " symbols
      print "$1\n";  # prints without " symbols
  }

thank you anyway!