# Recursive regular expressions in Ruby?

**URL:** <https://www.ruby-forum.com/t/recursive-regular-expressions-in-ruby/202534>\
**Category:** Ruby\
**Created:** [February 1, 2011, 12:06am UTC](https://www.ruby-forum.com/t/recursive-regular-expressions-in-ruby/202534 "2011-02-01T00:06:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 1, 2011, 12:06am UTC](https://www.ruby-forum.com/t/recursive-regular-expressions-in-ruby/202534/1 "2011-02-01T00:06:07Z")

</div>

Hi!

I was just wondering if there is possible to write recursive regular  
expressions in Ruby? As i’m understanding at this moment then there  
isn’t. Prove me wrong.

If you’re not aware of such possibility in Perl for example then here  
is one good blog post about it

> **[Recursive Regular Expressions](https://catonmat.net/recursive-regular-expressions)**
>
> The regular expressions we use in our daily lives are actually not that "regular." Most of the languages support some kind of extended regular expressions that are computationally more powerful than the "regular" regular expressions as defined by the...

It seems that also PHP has such a functionality.

Jarmo

---

<div class="post-metadata">

**Author:** ![juuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@juuser](https://www.ruby-forum.com/u/juuser)\
**Post date:** [February 1, 2011, 4:37am UTC](https://www.ruby-forum.com/t/recursive-regular-expressions-in-ruby/202534/2 "2011-02-01T04:37:43Z")

</div>

On Mon, Jan 31, 2011 at 5:05 PM, Jarmo P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> It seems that also PHP has such a functionality.
> 
> Jarmo

1.9.2 has it, here is an example:

regex = /^(?[(\g|\d)])$/  
‘1’.match regex # =\> nil  
‘[1]’.match regex # =\> #\<MatchData “[1]” bracketed:“[1]”\>  
‘[[1]]’.match regex # =\> #\<MatchData “[[1]]” bracketed:“[[1]]”\>  
‘[[[1]]]’.match regex # =\> #\<MatchData “[[[1]]]” bracketed:“[[[1]]]”\>

I found it in the Christmas edition of PragPub (  
[http://pragprog.com/magazines/download/19.PDF](http://pragprog.com/magazines/download/19.PDF)) they talk about it more  
in  
depth, as well, if you want more info.

---

<div class="post-metadata">

**Author:** ![juuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@juuser](https://www.ruby-forum.com/u/juuser)\
**Post date:** [February 1, 2011, 9:51am UTC](https://www.ruby-forum.com/t/recursive-regular-expressions-in-ruby/202534/3 "2011-02-01T09:51:12Z")

</div>

Thanks!

It seems that you’ve given wrong link as i’ve unable to find anything  
regarding “regular expressions” in that pdf.

Jarmo

---

<div class="post-metadata">

**Author:** ![juuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@juuser](https://www.ruby-forum.com/u/juuser)\
**Post date:** [February 1, 2011, 9:56am UTC](https://www.ruby-forum.com/t/recursive-regular-expressions-in-ruby/202534/4 "2011-02-01T09:56:15Z")

</div>

On Tue, Feb 1, 2011 at 2:50 AM, Jarmo P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Hi!
> > 
> > I found it in the Christmas edition of PragPub (  
> > [http://pragprog.com/magazines/download/19.PDF](http://pragprog.com/magazines/download/19.PDF)) they talk about it more in  
> > depth, as well, if you want more info.

You’re right, that was the January edition. Here is the one I meant to  
link  
to, Dec 2010 [http://pragprog.com/magazines/download/18.PDF](http://pragprog.com/magazines/download/18.PDF)

It is in section “what’s new in Ruby 1.9.2” on page 24

---

<div class="post-metadata">

**Author:** ![juuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@juuser](https://www.ruby-forum.com/u/juuser)\
**Post date:** [February 1, 2011, 9:39am UTC](https://www.ruby-forum.com/t/recursive-regular-expressions-in-ruby/202534/5 "2011-02-01T09:39:15Z")

</div>

On Tue, Feb 1, 2011 at 12:05 AM, Jarmo P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I was just wondering if there is possible to write recursive regular  
> expressions in Ruby? As i’m understanding at this moment then there  
> isn’t. Prove me wrong.
> 
> If you’re not aware of such possibility in Perl for example then here  
> is one good blog post about it  
> [Recursive Regular Expressions](http://www.catonmat.net/blog/recursive-regular-expressions)
> 
> It seems that also PHP has such a functionality.

Strictly speaking these are no more regular expressions any more  
because the set of languages that they can detect exceeds the set of  
regular languages. Normally you need at least a parser for a context  
free language to detect nested brackets etc. (There are quite a few  
parser generators available for Ruby.)

But: Oniguruma can do it, too:

Ruby version 1.9.2  
irb(main):001:0\> re = %r/((?((?:\[()]|[^()]|\g)_)))/  
=\> /((?((?:\[()]|[^()]|\g)_)))/  
irb(main):002:0\> s = ‘some(stri)((()x)(((c)d)e)))ng’  
=\> “some(stri\)\((()x)(((c)d)e)\))ng”  
irb(main):003:0\> mt = s.match re  
=\> #\<MatchData “(stri\)\((()x)(((c)d)e)\))”  
pg:“(stri\)\((()x)(((c)d)e)\))”\>  
irb(main):004:0\> mt[1]  
=\> “(stri\)\((()x)(((c)d)e)\))”

[http://www.siaris.net/index.cgi/+Programming/LanguageBits/Ruby/Oniguruma.rdoc](http://www.siaris.net/index.cgi/+Programming/LanguageBits/Ruby/Oniguruma.rdoc)

Kind regards

robert
