Ruby 是否支持条件正则表达式

2022-01-21 00:00:00 python regex conditional ruby

问题描述

只是一个语言功能问题,我知道在正则表达式(或多个正则表达式)之外有很多方法可以做到这一点.

Just a language feature question, I know there's plenty of ways to do this outside of regexes (or with multiple regexes).

ruby 是否支持 条件正则表达式?基本上,正则表达式中的 IF-THEN-ELSE 分支,其中 IF 的谓词是表达式中捕获的组的存在(或不存在).

Does ruby support conditional regular expressions? Basically, an IF-THEN-ELSE branch inside a regular expression, where the predicate for the IF is the presence (or absense) of a captured group in the expression.

在 perl 中,语法是 (?(predicate)yes-pattern|no-pattern)

In perl, the syntax is (?(predicate)yes-pattern|no-pattern) as in

/(?:y|(x))(?(1)y|x)/

我无法在我浏览过的任何 ruby​​ 文档中找到任何类似内容的参考,但我想我会在这里询问以确保,以防我搜索错误的单词,或者它已从文档中删除.

I haven't been able to find any reference to anything like it in any ruby docs that I've browsed, but I figured I'd ask here to make sure, in case I was searching for the wrong words, or it was elided from the docs.


解决方案

不,ruby 不支持(1.8 和 1.9 都不支持).

No, ruby does not support that (neither in 1.8 nor 1.9).

相关文章