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.
source share