Last active
April 10, 2018 11:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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