# Regular Expressions

**URL:** <https://www.ruby-forum.com/t/regular-expressions/66930>\
**Category:** Ruby\
**Created:** [August 30, 2006, 12:30pm UTC](https://www.ruby-forum.com/t/regular-expressions/66930 "2006-08-30T12:30:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 30, 2006, 12:30pm UTC](https://www.ruby-forum.com/t/regular-expressions/66930/1 "2006-08-30T12:30:48Z")

</div>

hi,

is there any way to reuse a group in a regular expression in ruby?

In other languages you can reuse a group by write (? …).

But I can’t figure out how to do this in ruby?

Do you have some advice?

Thanks  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 30, 2006, 1:32pm UTC](https://www.ruby-forum.com/t/regular-expressions/66930/2 "2006-08-30T13:32:57Z")

</div>

On 30.08.2006 12:29, Peter M. wrote:

> hi,
> 
> is there any way to reuse a group in a regular expression in ruby?
> 
> In other languages you can reuse a group by write (? …).
> 
> But I can’t figure out how to do this in ruby?
> 
> Do you have some advice?

Ruby’s current regexp engine does not support named groups. You can  
however reference by count:

> > /(b+)a+\1/ =~ (“aaabbb”\*10)  
> > =\> 3
> 
> > (“aaabbb”\*10).scan /(b+)a+\1/  
> > =\> [[“bbb”], [“bbb”], [“bbb”], [“bbb”], [“bbb”]]
> 
> > (“aaabbb”\*10).scan /((b+)a+\2)/  
> > =\> [[“bbbaaabbb”, “bbb”], [“bbbaaabbb”, “bbb”], [“bbbaaabbb”, “bbb”],  
> > [“bbbaaabbb”, “bbb”], [“bbbaaabbb”, “bbb”]]
> 
> >

HTH

```
robert
```

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 1, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/regular-expressions/66930/3 "2006-09-01T21:09:22Z")

</div>

Robert K. wrote:

> Ruby’s current regexp engine does not support named groups.

For the sake of providing complete information, Ruby’s next regexp  
engine (oniguruma) will. You can play around with it in the 1.9 branch  
(at least it was used there last time I checked.) I don’t know if you  
can use it standalone.

David V.
