[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] /AAA|BBB/



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