- 论坛徽章:
- 0
|
问题已解决。
perldoc perlretut
Grouping things and hierarchical matching
Extracting matches
这里面有详细的说明。摘抄如下:
===================================================
Extracting matches
The grouping metacharacters "()" also serve another completely differ-
ent function: they allow the extraction of the parts of a string that
matched. This is very useful to find out what matched and for text
processing in general. For each grouping, the part that matched inside
goes into the special variables $1, $2, etc. They can be used just as
ordinary variables:
# extract hours, minutes, seconds
$time =~ /(\d\d) \d\d) \d\d)/; # match hh:mm:ss format
$hours = $1;
$minutes = $2;
$seconds = $3;
Now, we know that in scalar context, "$time =~ /(\d\d) \d\d) \d\d)/"
returns a true or false value. In list context, however, it returns
the list of matched values "($1,$2,$3)". So we could write the code
more compactly as
# extract hours, minutes, seconds
($hours, $minutes, $second) = ($time =~ /(\d\d) \d\d) \d\d)/);
If the groupings in a regexp are nested, $1 gets the group with the
leftmost opening parenthesis, $2 the next opening parenthesis, etc.
================================================== |
|