September 2017
Beginner
402 pages
9h 52m
English
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 and see what it prints:
⌈index.html⌋ ...