Conditional regular expression with Perl

perl, regex

Solution

Perl supports conditional patterns.

Your regex doesn't just not match, it throws the following syntax error:

Unknown switch condition (?(A) in regex; marked by <-- HERE in m/(?( <-- HERE A)B|C)/

That's because `A` is not a valid condition.

Problem

Does Perl support conditional regular expression : ``` (?(condition)true-pattern|false-pattern) ``` i.e. if the condition is true then try to match the true pattern else try to match the false pattern If Perl supported conditional regular expressions then why didn't this code print `1`? ``` use strict; use warnings; $_ = 'AB'; if ( /(?(A)B|C)/ ) { print 1; } ```

Original source