Skip to content

Instantly share code, notes, and snippets.

@DavidSzczesniak
Last active April 10, 2018 11:34
Show Gist options
  • Save DavidSzczesniak/9507c2c9fcc760bcbd79c1184f3e4a45 to your computer and use it in GitHub Desktop.
Save DavidSzczesniak/9507c2c9fcc760bcbd79c1184f3e4a45 to your computer and use it in GitHub Desktop.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment