string matching doesn't work

J

Jack

Hello, I have a little problem. i am trying to extract some text from
a file. I need to get the first names and last names as well as the
data between the <TD> tags which corresponds to Current Status. For
example:

<h1>Mr. firstname lastname</h1>
<th class="profile"><nobr>Current Status</nobr></th><td
class="profile" colspan="2">Some data</td>

so i need firstname lastname as well as some data. my current string
looks like this but it doesn't work.

if (@content =~ m/<h1>Mr.*<\/h1>/){
print "here " . $1;
}

Please help....
 
M

Mirco Wahab

Jack said:
Hello, I have a little problem. i am trying to extract some text from
a file. I need to get the first names and last names as well as the
data between the <TD> tags which corresponds to Current Status. For
example:

<h1>Mr. firstname lastname</h1>
<th class="profile"><nobr>Current Status</nobr></th><td
class="profile" colspan="2">Some data</td>
so i need firstname lastname as well as some data. my current string
looks like this but it doesn't work.
if (@content =~ m/<h1>Mr.*<\/h1>/){
print "here " . $1;
}


If you use this more often and in a production
environment, you might be better of after
learning one of the HTML-Parser modules:

http://search.cpan.org/~gaas/HTML-Parser-3.56/

But if its only an occasional thing to do,
you can built a regular expression for
extraction, like:

...

my $html='
<h1>Mr. firstname lastname</h1>
<th class="profile"><nobr>Current Status</nobr></th>
<td class="profile" colspan="2">Some data</td>
';

my $expr = qr{
<h1> ([^<]+) </h1> # stuff in h1 => $1
.+? # jump to the next td
<td[^>]+> ([^<]+) </td> # stuff in td => $2
}sx;


print "h1 => |$1|, td => |$2|\n" while $html =~ /$expr/g;

...

Regards

M.
 
L

Lars Eighner

In our last episode,
the lovely and said:
Hello, I have a little problem. i am trying to extract some text from
a file. I need to get the first names and last names as well as the
data between the <TD> tags which corresponds to Current Status. For
example:
<h1>Mr. firstname lastname</h1>
<th class="profile"><nobr>Current Status</nobr></th><td
class="profile" colspan="2">Some data</td>
so i need firstname lastname as well as some data. my current string
looks like this but it doesn't work.
if (@content =~ m/<h1>Mr.*<\/h1>/){
print "here " . $1;
}
Please help....


perldoc -q matching
 
M

Mahesh Asolkar

Hello, I have a little problem. i am trying to extract some text from
a file. I need to get the first names and last names as well as the
data between the <TD> tags which corresponds to Current Status. For
example:

<h1>Mr. firstname lastname</h1>
<th class="profile"><nobr>Current Status</nobr></th><td
class="profile" colspan="2">Some data</td>

so i need firstname lastname as well as some data. my current string
looks like this but it doesn't work.

if (@content =~ m/<h1>Mr.*<\/h1>/){
print "here " . $1;
}

Please help....

I would use something like the HTML::TreeBuilder module to extract
information out of HTML data.

If you want to stick with pattern matching, following should be
informative:

perldoc perlretut (or http://perldoc.perl.org/perlretut.html)
perldoc perlre (or http://perldoc.perl.org/perlre.html)

HTH,
Mahesh.
 
D

Dr.Ruud

Jack schreef:
if (@content =~ m/<h1>Mr.*<\/h1>/){
print "here " . $1;
}

$ perl -le'
# use warnings;
@c = qw(abc de fghi);
if (@c =~ /([bdg0-9])/) {
print $1;
}
'
3
 
J

Jack

Jack schreef:
if (@content =~ m/<h1>Mr.*<\/h1>/){
print "here " . $1;
}

$ perl -le'
# use warnings;
@c = qw(abc de fghi);
if (@c =~ /([bdg0-9])/) {
print $1;
}
'
3



I used Mirco Wahab's solution and it worked for some of the cases but
now I am stuck at parsing this line.

<td class="profile">Address<br>City&nbsp;State&nbsp;&nbsp;V2X
6J3<br>Canada<br>

</td><td align="right" class="profile"><nobr><b>Phone: </
b>555-555-5555</nobr>&nbsp;
</td>


I want to parse each section into a variable using qr. so for the
Address I used
<td[^>]+> ([^<]+) <br> # this line works fine and I get the address
portion
.+?
<br> ([^&]+) &nbsp\; # this part doesn't work meaning that I
don't get anything for city

Any help would much appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,677
Latest member
MoisesKoeh

Latest Threads

Top