# Accessing all matches using regex

**URL:** <https://www.ruby-forum.com/t/accessing-all-matches-using-regex/111334>\
**Category:** Ruby\
**Created:** [August 6, 2007, 6:05am UTC](https://www.ruby-forum.com/t/accessing-all-matches-using-regex/111334 "2007-08-06T06:05:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [August 6, 2007, 6:05am UTC](https://www.ruby-forum.com/t/accessing-all-matches-using-regex/111334/1 "2007-08-06T06:05:38Z")

</div>

Hey all

so, I primarily use PERL for creating shell scripts but I’m trying to  
learn ruby since it feels like it could do more of what I want with  
cleaner code. I especially like the way it has regex built right in.

I’m running into an issue though, because in perl, I can do this:

while (/href="([^"]+)"/) {

## do stuff…

}

I use this simple regex because it’s based around what I use this style  
for the most.

I can’t figure out how to that in ruby. how can I iterate over all  
matches in a string? I could split the lines, but sometimes there may be  
two or more matches on a single line.

is there a nice, clean, ruby way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [August 6, 2007, 6:32am UTC](https://www.ruby-forum.com/t/accessing-all-matches-using-regex/111334/2 "2007-08-06T06:32:14Z")

</div>

On 8/5/07, Spike G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> while (/href="([^"]+)"/) {
> 
> ## do stuff…
> 
> }  
> …  
> I can’t figure out how to that in ruby. how can I iterate over all  
> matches in a string? I could split the lines, but sometimes there may be  
> two or more matches on a single line.
> 
> is there a nice, clean, ruby way to do this?

Have a look at String#scan (“ri String#scan” might do it, depending  
upon your installation).  
For example,

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [August 6, 2007, 6:36am UTC](https://www.ruby-forum.com/t/accessing-all-matches-using-regex/111334/3 "2007-08-06T06:36:14Z")

</div>

> is there a nice, clean, ruby way to do this?

Have a look at String#scan

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [August 6, 2007, 7:53am UTC](https://www.ruby-forum.com/t/accessing-all-matches-using-regex/111334/4 "2007-08-06T07:53:18Z")

</div>

look up gsub! for the String class. you can write

some\_string.gsub(/href="([^"]+)"/) do |match|  
## do stuff with match  
end

i’m pretty sure the above code will work for what you are trying to do.

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [August 6, 2007, 2:24pm UTC](https://www.ruby-forum.com/t/accessing-all-matches-using-regex/111334/5 "2007-08-06T14:24:25Z")

</div>

david karapetyan wrote:

> look up gsub! for the String class. you can write
> 
> some\_string.gsub(/href="([^"]+)"/) do |match|  
> ## do stuff with match  
> end
> 
> i’m pretty sure the above code will work for what you are trying to do.

you know, I thought about String#scan, but for some reason didn’t think  
it was what I was looking for, but it works sorta nicely. it doesn’t  
behave exactly how I’d want… but the String#gsub method looks more  
like what I’m looking for; this way, I can loop and access my various  
$1, $2, etc matches and not have to work with an array that I have to  
loop over in a weird way (since I don’t use some of matches).

I’ll have to play with this a little more.

thanks!
