Regexp question

L

Lyndon Samson

I have a HTML document containing many table cells of which I wish to
extract the contents.


r = Regexp.new("\<TD.*?\>(.*?)\<\TD\>", Regexp::MULTILINE)
m = r.match(table)

The above only matches the first cell, I'd like to continue the match
finding each subsequent cell. The not-very-nice way to do this is to
take the char offset of the match, create a new string from that point
and feed it back into match.

Whats the better way?
 
R

Robert Klemme

Lyndon said:
I have a HTML document containing many table cells of which I wish to
extract the contents.


r = Regexp.new("\<TD.*?\>(.*?)\<\TD\>", Regexp::MULTILINE)
m = r.match(table)

The above only matches the first cell, I'd like to continue the match
finding each subsequent cell. The not-very-nice way to do this is to
take the char offset of the match, create a new string from that point
and feed it back into match.

Whats the better way?

Use String#scan.

robert
 
S

Shajith

------=_Part_8740_26768011.1115639827967
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

=20
The above only matches the first cell, I'd like to continue the match
finding each subsequent cell.


Have you tried scan? Try the same regexp with String#scan, to get an array=
=20
of matched groups

- Shajith.

------=_Part_8740_26768011.1115639827967--
 
N

Nikolai Weibull

Lyndon Samson, May 9:
r = Regexp.new("\<TD.*?\>(.*?)\<\TD\>", Regexp::MULTILINE)
m = r.match(table)

The above only matches the first cell, I'd like to continue the match
finding each subsequent cell. The not-very-nice way to do this is to
take the char offset of the match, create a new string from that point
and feed it back into match.
Whats the better way?

Using String#scan, as previously suggested, is the easiest method.
Another way of doing it is to use a loop while m is non-nil and match
against m.post_match on each iteration. See the documentation of the
MatchData class for more information,
nikolai
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top