使用诸如perl之类的递归正则表达式匹配Ruby中的平衡括号 [英] Matching balanced parenthesis in Ruby using recursive regular expressions like perl

查看:124
本文介绍了使用诸如perl之类的递归正则表达式匹配Ruby中的平衡括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种在正则表达式中匹配平衡括号的方法,并在Perl中找到了一种使用递归正则表达式的方法:

I have been looking for a way to match balanced parenthesis in a regex and found a way in Perl, that uses a recursive regular expression:

my $re;
$re = qr{
           \(
              (?:
                 (?> [^()]+ )       # Non-parens without backtracking
                 |
                 (??{ $re })        # Group with matching parens
              )*
           \)
         }x;

来自 perl正则表达式网站

有没有办法用Ruby或类似语言来做到这一点?

Is there a way to do this in Ruby or a similar language?

UPDATE :

UPDATE:

对于那些感兴趣的人,有一些有趣的链接:

For those interested here are some interesting links:

Oniguruma手册-从Sawa的答案中获得.

Oniguruma manual - from Sawa's answer.

实用程序员的Ruby 1.9正则表达式示例章节

推荐答案

是.使用 oniguruma regex引擎(它内置在Ruby 1.9中,并且可以在Ruby 1.8上安装),您可以做到这一点.您可以用(?<name>...)(?'name'...)命名子正则表达式.然后,您可以在同一正则表达式中使用\g<name>\g'name'调用子正则表达式.因此,将您翻译成oniguruma regex的正则表达式为:

Yes. With oniguruma regex engine, which is built in in Ruby 1.9, and is installable on Ruby 1.8, you can do that. You name a subregex with (?<name>...) or (?'name'...). Then you call a subregex with \g<name> or \g'name' within the same regex. So your regex translated to oniguruma regex will be:

re = %r{
  (?<re>
    \(
      (?:
        (?> [^()]+ )
        |
        \g<re>
      )*
    \)
  )
}x

还请注意,PHP> = 5中的多字节字符串模块使用oniguruma regex引擎,因此您可以执行相同操作.

Also note that multi-byte string module in PHP >=5 uses oniguruma regex engine, so you will be able to do the same.

有关oniguruma的手册,请此处.

The manual for oniguruma is here.

这篇关于使用诸如perl之类的递归正则表达式匹配Ruby中的平衡括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆