# Is there something like Regexp#match\_all

**URL:** <https://www.ruby-forum.com/t/is-there-something-like-regexp-match-all/79057>\
**Category:** Ruby\
**Created:** [December 11, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/is-there-something-like-regexp-match-all/79057 "2006-12-11T22:26:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![schacke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@schacke](https://www.ruby-forum.com/u/schacke)\
**Post date:** [December 11, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/is-there-something-like-regexp-match-all/79057/1 "2006-12-11T22:26:44Z")

</div>

Hi,

is there a possibility to return all the matches of a regexp? or in  
other words is there a way to apply a block to all matches at once?  
Something like String#gsub but where I can specifiy what to do with the  
matches…

Thanks  
Andi

---

<div class="post-metadata">

**Author:** ![schacke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@schacke](https://www.ruby-forum.com/u/schacke)\
**Post date:** [December 11, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/is-there-something-like-regexp-match-all/79057/2 "2006-12-11T22:38:06Z")

</div>

On 12/11/06, Andi S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> is there a possibility to return all the matches of a regexp? or in  
> other words is there a way to apply a block to all matches at once?  
> Something like String#gsub but where I can specifiy what to do with the  
> matches…

These things are actually two different things.

1. Look at String#scan
2. Look at String#gsub with the block form and use Regexp::last\_match  
(it’s a thread-local value, IIRC).

-austin

---

<div class="post-metadata">

**Author:** ![schacke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@schacke](https://www.ruby-forum.com/u/schacke)\
**Post date:** [December 11, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/is-there-something-like-regexp-match-all/79057/3 "2006-12-11T23:40:23Z")

</div>

On 11.12.2006 22:26, Andi S. wrote:

> is there a possibility to return all the matches of a regexp? or in  
> other words is there a way to apply a block to all matches at once?  
> Something like String#gsub but where I can specifiy what to do with the  
> matches…

What exactly do you want to do with matches?

```
robert
```

---

<div class="post-metadata">

**Author:** ![schacke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@schacke](https://www.ruby-forum.com/u/schacke)\
**Post date:** [December 11, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/is-there-something-like-regexp-match-all/79057/4 "2006-12-11T23:58:26Z")

</div>

On Dec 11, 2006, at 4:50 PM, Andi S. wrote:

> > robert
> 
> I’d like to build a small web-tool for myself to highlight all the  
> matches of a regexp against a specified string (e.g. the matches  
> should  
> be in a different color). But I think I found a solution:
> 
> source\_string.gsub(regexp) {|match| “\<span  
> style=“color:red;”\>#{match}”}

You don’t really need the block form for such a simple replacement.  
This is the same thing:

source\_string.gsub(regexp, ‘&’)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![schacke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@schacke](https://www.ruby-forum.com/u/schacke)\
**Post date:** [December 11, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/is-there-something-like-regexp-match-all/79057/5 "2006-12-11T23:50:56Z")

</div>

Robert K. wrote:

> On 11.12.2006 22:26, Andi S. wrote:
> 
> > is there a possibility to return all the matches of a regexp? or in  
> > other words is there a way to apply a block to all matches at once?  
> > Something like String#gsub but where I can specifiy what to do with the  
> > matches…
> 
> What exactly do you want to do with matches?
> 
> robert

I’d like to build a small web-tool for myself to highlight all the  
matches of a regexp against a specified string (e.g. the matches should  
be in a different color). But I think I found a solution:

source\_string.gsub(regexp) {|match| “\<span  
style=“color:red;”\>#{match}”}

---

<div class="post-metadata">

**Author:** ![schacke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@schacke](https://www.ruby-forum.com/u/schacke)\
**Post date:** [December 12, 2006, 11:15am UTC](https://www.ruby-forum.com/t/is-there-something-like-regexp-match-all/79057/6 "2006-12-12T11:15:47Z")

</div>

On 11.12.2006 23:56, James Edward G. II wrote:

> source\_string.gsub(regexp, ‘&’)

… and in fact more efficient IIRC.

```
robert
```
