# Parsing HTML using regexes and arrays

**URL:** <https://www.ruby-forum.com/t/parsing-html-using-regexes-and-arrays/152834>\
**Category:** Ruby\
**Created:** [November 7, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/parsing-html-using-regexes-and-arrays/152834 "2008-11-07T22:10:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![soldier.coder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@soldier.coder](https://www.ruby-forum.com/u/soldier.coder)\
**Post date:** [November 7, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/parsing-html-using-regexes-and-arrays/152834/1 "2008-11-07T22:10:56Z")

</div>

I have a nice little regex to pull the information rich guts from a  
table…

%r{\</thead._?\>(._?)}m =~html

# $1 now contains all the rows of the table as one long string.

I’d like to turn that into an array of rows, but I am not exactly sure  
how.

Additionally, I’d like to process the rows so that i can get data from  
between the nth

 pair.

Any help?

---

<div class="post-metadata">

**Author:** ![soldier.coder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@soldier.coder](https://www.ruby-forum.com/u/soldier.coder)\
**Post date:** [November 7, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/parsing-html-using-regexes-and-arrays/152834/2 "2008-11-07T23:36:02Z")

</div>

On Fri, Nov 7, 2008 at 3:08 PM, soldier.coder  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> between the nth pair.
> 
> Any help?

If you have a string with a repeating pattern that you want an array  
of, String#scan is your man.

irb(main):001:0\> html = “foobar”  
=\> “foobar”  
irb(main):002:0\> a = html.scan(/(.+?)\</td\>/)  
=\> [[“foo”], [“bar”]]

Hmmm, that’s sort of ugly.

irb(main):003:0\> a = html.scan(/(.+?)\</td\>/).flatten  
=\> [“foo”, “bar”]

Much better.

Ad hoc regexes are fine for quick-n-dirty scripting. But if you’re  
serious about parsing HTML you might want to look into Hpricot or  
Nokogiri.

-Michael L.
