#!/usr/bin/perl -w
=pod
命名捕获--给匹配上的内容加上标签
捕获到的内容都会保存在%+散列中,这个散列的key为对应的标签;
方便之处就是利于程序扩展和阅读,不用繁琐的一个一个去数括号来获取匹配变量
=cut
zjtest7-frontend:/root/perl# cat a2.pl
use strict;
my $str = "jack and rose";
if ($str =~ /(?<first>\S+) (and|or) (?<second>\S+)/) {
my ($first, $second) = ($+{first}, $+{second});
print "$first\n$second\n"; # jack, rose
}
zjtest7-frontend:/root/perl# perl a2.pl
jack
rose
\s 空格,和 [\n\t\r\f] 语法一样
\s+ 和 [\n\t\r\f]+ 一样
\S 非空格,和 [^\n\t\r\f] 语法一样
\S+ 和 [^\n\t\r\f]+ 语法一样
/******************************************************
zjtest7-frontend:/root/perl# cat a1.pl
my $str="begin 123.456 end";
if ($str =~/(?<first>\S+)\s+(?<second>\S+)\s+(?<third>\S+)/)
{
my ($first, $second,$third) = ($+{first}, $+{second},$+{third});
print "$first\n$second\n$third\n"; # jack, rose
}
zjtest7-frontend:/root/perl# perl a1.pl
begin
123.456
end
zjtest7-frontend:/root/perl# cat a3.pl
my $str="begin 123.456 end";
if ($str =~/\s+(?<request_time>\d+(?:\.\d+)?)\s+/){
my ($request_time) = ($+{request_time});
print $request_time."\n";};
zjtest7-frontend:/root/perl# perl a3.pl
123.456
perl 正则命名捕获
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:AngularJS之有序列表
下一篇:perl 继承写法
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【C++】详解命名空间
【C++】C++学习篇(1)——命名空间详细介绍C++中的命名空间,并深度剖析使用命名空间需要注意的三个方面……
命名空间 C++ 作用域 生命周期 -
Python 函数命名最佳实践
Python 函数命名最佳实践
python Python 内置函数 -
Java:正则表达式的命名捕获组
【代码】Java:正则表达式的命名捕获组。
java 正则表达式 开发语言 System