Just a language feature question, I know there's plenty of ways to do this outside of regexes (or with multiple regexes).
只是一个语言功能问题,我知道有很多方法可以在正则表达式(或多个正则表达式)之外执行此操作。
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.
ruby是否支持条件正则表达式?基本上,正则表达式中的IF-THEN-ELSE分支,其中IF的谓词是表达式中捕获的组的存在(或缺失)。
In perl, the syntax is (?(predicate)yes-pattern|no-pattern)
as in
在perl中,语法是(?(谓词)yes-pattern | no-pattern),如
/(?:y|(x))(?(1)y|x)/
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文档中,我都找不到任何类似的东西,但我想我会在这里要求确保,以防我在搜索错误的单词,或者它被删除了来自文档。
2 个解决方案
#1
4
No, ruby does not support that (neither in 1.8 nor 1.9).
不,ruby不支持(1.8和1.9都不支持)。
#2
-1
FWIW, Python started to support this starting in 2.4: http://docs.python.org/library/re#regular-expression-syntax
FWIW,Python从2.4开始支持这个:http://docs.python.org/library/re#regular-expression-syntax
#1
4
No, ruby does not support that (neither in 1.8 nor 1.9).
不,ruby不支持(1.8和1.9都不支持)。
#2
-1
FWIW, Python started to support this starting in 2.4: http://docs.python.org/library/re#regular-expression-syntax
FWIW,Python从2.4开始支持这个:http://docs.python.org/library/re#regular-expression-syntax