Regular Expression Help

S

Saya

Hi,
here is the scenario:

I have a variable containing the following:
<table cellpadding="0" cellspacing="0">
<tr>
<td class="footer">Ottiliavej 9</td>
<td class="footer">telefon 092323</td></tr>
<tr><td class="footer">DK-2500 Valby</td>
<td class="footer">fax 092323</td>
</tr>
</table>

A regular expression that gives me the text(sometext) between the <td
....>sometext</td>
tags ?
I have tried all of my own knowledge og regular expression (which by
the way is very little). Any help will be appeciated.

Thx
Saya
 
V

Vlad Tepes

Saya said:
Hi,
here is the scenario:

I have a variable containing the following:
<table cellpadding="0" cellspacing="0">
<tr>
<td class="footer">Ottiliavej 9</td>
<td class="footer">telefon 092323</td></tr>
<tr><td class="footer">DK-2500 Valby</td>
<td class="footer">fax 092323</td>
</tr>
</table>

A regular expression that gives me the text(sometext) between the <td
...>sometext</td>
tags ?
I have tried all of my own knowledge og regular expression (which by
the way is very little). Any help will be appeciated.

Here is one way (assuming the html is stored in $html):

my @tds = $html =~ m#<td class="footer">(.*)</td>#g;

( Matching (=~) has higher precedence than assignment (=), so
the list of captured items by matching $html against the regex
is put into the array @tds. )

To handle cells with newlines, for example

<td class="footer">telefon
092323</td></tr>

you could make the period match newlines (add the modifier 's'), and
then use minimal matching (a questionmark after the quantifier makes it
eat as few characters as possible, ie. find the first '</td>' instead of
the last) thus:

my @tds = $html =~ m#<td class="footer">(.*?)</td>#sg;

Some references:

perldoc perlrequick (gentle intro to regexes)
perldoc perlre (the full story)
perldoc -q html (search faqs for html,
could be of interest)
 

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

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,302
Latest member
MitziWragg

Latest Threads

Top