Capturing
Learn how to group and capture different portions of the match.
We'll cover the following...
Regular expressions allow us to group and capture portions of the match for later use. To extract an American telephone number of the form (202) 456-1111 from a string, use this:
Press + to interact
my $area_code = qr/\(\d{3}\)/;my $local_number = qr/\d{3}-?\d{4}/;my $phone_number = qr/$area_code\s?$local_number/;
Note: Note the escaped parentheses within
$area_code
. Parentheses are special in Perl regular expressions. They group atoms into larger units and capture portions of matching strings. To match literal parentheses, escape them with backslashes as ...