[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp question

Lyndon Samson

5/9/2005 11:43:00 AM

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?


--
Into RFID? www.rfidnewsupdate.com Simple, fast, news.



3 Answers

Robert Klemme

5/9/2005 11:55:00 AM

0

Lyndon Samson wrote:
> 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

Shajith

5/9/2005 11:57:00 AM

0

On 5/9/05, Lyndon Samson <lyndon.samson@gmail.com> wrote:
>
> 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
of matched groups

- Shajith.

Nikolai Weibull

5/9/2005 12:14:00 PM

0

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

--
Nikolai Weibull: now available free of charge at http:/...!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}