Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Answer: How can I find the contents of an HTML tag?

by nuance (Hermit)
on May 09, 2000 at 15:30 UTC ( #10739=categorized answer: print w/replies, xml ) Need Help??

Q&A > data formatting > How can I find the contents of an HTML tag? - Answer contributed by nuance

You could use a regular expression with the /g modifier:

m#<TD>(.*?)</TD>#g

This will return a list of all the matches.

You can then select whichever term you want from the list:

my $html = "<TR><TD>Foo:</TD><TD> bar </TD></TR>"; my $var = ( $html =~ m#<TD>(.*?)</TD>#g )[1];

Note that you have to modify the asterisk with the question mark to specify non-greedy matching. If you don't, you will get just one big match, like

Foo:&lt;/TD&gt;&lt;TD&gt; bar
— probably not what you wanted!

You should also add the /s modifier, if the string you need to extract breaks across multiple lines. Specifically, /s allows the dot to match newline characters along with all the other characters.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2017-06-28 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (632 votes). Check out past polls.