Does Ruby Support Conditional Regular Expressions

The question is only about the language, I know that there are many ways to do this outside of regular expressions (or with multiple regular expressions).

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

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

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

I could not find any link to anything like that in any ruby ​​documents that I looked through, but I decided that I would ask here to make sure that if I was looking for the wrong words or it was excluded from the documents.

+4
source share
2 answers

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

+3
source

FWIW, Python started supporting this run in 2.4: http://docs.python.org/library/re#regular-expression-syntax

-1
source

Source: https://habr.com/ru/post/953662/


All Articles