Perl正則表達的特殊變量

 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:最後結束的括號匹配文本;
@-:匹配開始位置的偏移值數組;
@+:匹配結束位置的偏移值數組;
相關文章
相關標籤/搜索