Perl正则表达的特殊变量
#!/usr/bin/perl
use 5.010;
$name = "the age of caoqing is 100.";
if ($name =~ m/(\w+)(\s+)(\d+(\.))/) {
printf("the age is %s\n", $&);
printf("the before is %s\n", $`);
printf("the after is %s\n", $');
say $1;
say $+;
say $^N;
say "@-";
say "@+";
}
# ./test.pl
the age is is 100.
the before is the age of caoqing
the after is
is
.
100.
19 19 21 22 25
26 21 22 26 26
$`:匹配文本之前的文本;
$&:匹配文本;
$':匹配文本之后的文本;
$1:第一个分组捕获;
$2:第二个分组捕获;
...
$+:编号最大的括号匹配文本;
$^N:最后结束的括号匹配文本;
@-:匹配开始位置的偏移值数组;
@+:匹配结束位置的偏移值数组;
转载于:https://my.oschina.net/u/1449160/blog/291881