[Omaha.pm] /AAA|BBB/

Jay Hannah jay at jays.net
Thu Feb 2 08:25:41 PST 2006


Andy Lester wrote:
>>(2) What does this print (what does adding parens change)?:
> 
> Adding parens doesn't change anything as far as what matches or  
> doesn't.  It only captures the match.

Sure it does, sometimes. Parens allow for multi-character control of the | character while matching. That's why I always use them. 

$ cat j.pl

foreach (qw(AAXXBB AAYBB AAXYBB AAYBB AYBB)) {
   printf("%-7s", $_);
   print (($_ =~ /^AA(XX|Y)BB$/) ? "yes  " : "no   ");
   print (($_ =~ /^AAXX|YBB$/)   ? "yes  " : "no   ");
   print "\n";
}

$ perl j.pl
AAXXBB yes  yes
AAYBB  yes  yes
AAXYBB no   yes
AAYBB  yes  yes
AYBB   no   yes

j




More information about the Omaha-pm mailing list