# Using regular expressions

**URL:** <https://www.ruby-forum.com/t/using-regular-expressions/153048>\
**Category:** Ruby\
**Created:** [November 11, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/using-regular-expressions/153048 "2008-11-11T14:25:52Z")\
**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 11, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/using-regular-expressions/153048/1 "2008-11-11T14:25:52Z")

</div>

I have the following code:

require ‘open-uri’  
def scrape\_table(html)  
%r{\</thead._?\>(._?)}m =~html  
$1  
end

def scrape\_case(a\_line)  
%r{(\<a\s.\*?\d{6}-\d{2}’\>\d{6}-\d{2}\</a\>)}m =~ a\_line  
$1  
end

if $0 == **FILE**

```
url = 'http://localhost:8080/tests/raw.html';
page = open(url) #open the url like a file
text = page.read; #read it into one string
my_table = scrape_table(text) #grab or "scrape" the table
my_link = scrape_case(my_table) #grab a html that includes a 6-2

```

digit number (ex: 080910-15)  
puts(my\_table) #prints out my\_table – which contains the table  
information  
puts("\n")  
puts(my\_link)

end

The code grabs the one table contained in my URL then looks for an  
HTML link that includes a number that is 6 digits, followed by a dash,  
followed by 6 digits. I’m fairly certain the regex in scrape\_case( )  
grabs more than one html link, if more than one is in the table. Is  
there any way I can grab all those links into an array?

---

<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 11, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/using-regular-expressions/153048/2 "2008-11-11T14:53:26Z")

</div>

On 2008.11.11., at 14:22, soldier.coder wrote:

> $1  
> end

> Is there any way I can grab all those links into an array?

Sure - String#scan is your friend:

def scrape\_case(a\_line)  
a\_line.scan(/\<a\s.\*?\d{6}-\d{2}’\>\d{6}-\d{2}\</a\>/)  
end

ex:

> > “123456-78 here is another: 111111-99”.scan(/\<a\s.\*?\d{6}-\d{2}’\>\d{6}-\d{2}\<  
> > /a\>/)  
> > =\> [“123456-78”, “111111-99”]

HTH,  
Peter
