Extracting substrings with capturing
Matching against regexes is not enough. The real power of regular expressions is not complete without the ability to extract the substrings that agreed with the regex pattern. Saving the parts of the string in special variables is called capturing.
Capturing groups
In Perl 6, capturing is achieved by placing the part of a regex in parentheses. Parentheses have as dual meaning in regexes. We already have seen the usage of parentheses for grouping alternatives in the phone number.
Let us continue with the example of extracting values of HTML attributes. We want now to print the values. So, we need to create a regex and mark the borders of the data that we want to extract. Captured data is put into the variables $0
, $1
, and so on. Numerical indices start with zero and correspond to the order number of capturing parentheses in the regex:
my $str = q{<a href="index.html" class="menu">}; $str ~~ / \" (.*?) \" .* \" (.*?) \" /; say $0; say $1;
Run this code...