DavidSzczesniak
4/10/2018 - 11:12 AM

Noncapturing Parentheses

Provides a way to use parentheses to grop things without triggering the capture groups.

# Example: 

# Before - bronto is $1 and what we want is $2. This will get confusing in more complicated patterns
if ((/bronto)?saurus (steak|burger)/) {
  print "Fred wants $2\n";
}

# After - now using noncapturing parentheses around bronto
if (/(?:bronto)?saurus (steak|burger)/) {
  print "Fred wants $1\n";
}

# (v5.22) To turn all parentheses into noncapturing groups use the \n flag:
if (/(?:bronto)?saurus (steak|burger)/n) {
  print "It matched\n"; # there is no $1 now
}