m/(?(?=Z|Y)[Z]|[Y])/ #### (?(DEFINE) (?....) ) #### print "Yes" if $string =~ m/(?(?=')'[^']|) (?(?=")"[^"]|)/x; #### m/(?:'[^']|"[^"])/