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
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
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)\))”