if ( $value =~ m/^(?: [AN]+ # Either at least one A|N |[AN]* # or an A|N run (?: NP)? # Possibly interrupted by NP [AN]* ) N$/x ){ # With a terminal N. print "match: $value\n"; }