# Regexp question

**URL:** <https://rubytalk.org/t/regexp-question/18244>\
**Category:** ruby-talk\
**Created:** [9 May 2005 11:43 UTC](https://rubytalk.org/t/regexp-question/18244 "2005-05-09T11:43:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lyndon\_Samson](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@Lyndon\_Samson](https://rubytalk.org/u/Lyndon_Samson)\
**Post date:** [9 May 2005 11:43 UTC](https://rubytalk.org/t/regexp-question/18244/1 "2005-05-09T11:43:20Z")

</div>

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](http://www.rfidnewsupdate.com) Simple, fast, news.

---

<div class="post-metadata">

**Author:** ![CT1](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CT1](https://rubytalk.org/u/CT1)\
**Post date:** [9 May 2005 11:57 UTC](https://rubytalk.org/t/regexp-question/18244/2 "2005-05-09T11:57:28Z")

</div>

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

- Shajith.

> **···**
>
> 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.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [9 May 2005 11:59 UTC](https://rubytalk.org/t/regexp-question/18244/3 "2005-05-09T11:59:27Z")

</div>

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.

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Nikolai\_Weibull](https://avatars.discourse-cdn.com/v4/letter/n/ce7236/32.png) [@Nikolai\_Weibull](https://rubytalk.org/u/Nikolai_Weibull)\
**Post date:** [9 May 2005 12:13 UTC](https://rubytalk.org/t/regexp-question/18244/4 "2005-05-09T12:13:43Z")

</div>

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,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

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