\s+(?<request_time>\d+(?:\.\d+)?)\s+ 解释

<pre name="code" class="html"><pre name="code" class="html">看elk 看到一个正则;
\s+(?<request_time>\d+(?:\.\d+)?)\s+

回顾下:
(?:pattern)
 匹 配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 “或” 字符 (|) 来组合一个模式的各个部分是很有用。例如, ‘industr(?:y|ies) 就是一个比 ‘industry|industries’ 更简略的表达式。

表示括号只是起分隔作用,不将括号中匹配的内容存入内存中
因为如果不加?:的话,括号内匹配的内容会放入$1,$2...这些变量中

1、命名分组格式为(?<grp name>),反向引用时用\k<grp name>

2、命名分组的匹配的结果存在在变量%+变量中,取命名分组值,$+{grp name}.


[tomcat@wx02 test]$ cat a2.pl 
my $str="456.123a";
if ($str=~/\d+(\.\d+)/){print "\$1 is $1\n"};
[tomcat@wx02 test]$ perl a2.pl 
$1 is .123


[tomcat@wx02 test]$ cat a2.pl 
my $str="456.123a";
if ($str=~/\d+(\.\d+)?/){print "\$1 is $1\n"};
[tomcat@wx02 test]$ perl a2.pl 
$1 is .123

\d+(\.\d+)? 中的?有什么用呢?


x?  匹配 0 次或一次 x 字符串 


[tomcat@wx02 test]$ cat a2.pl 
my $str="456";
if ($str=~/\d+(\.\d+)?/){print "\$1 is $1\n"};
[tomcat@wx02 test]$ perl a2.pl 
$1 is 
[tomca

这里的(\.\d+)? 就是0次或者1次
zjtest7-frontend:/root/test# cat a1.pl 
my $str="456.123a";
if ($str=~/\d+(?:\.\d+)?/){print "\$1 is $1\n"};
zjtest7-frontend:/root/test# perl a1.pl 
$1 is




   

posted @ 2016-08-30 22:38  czcb  阅读(1018)  评论(0编辑  收藏  举报