Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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:


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?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-01-23 14:55 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (248 votes). Check out past polls.