perl 正则命名捕获
2016-08-19 09:46
190 查看
#!/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正则命名分组捕获
- perl正则的命名捕获与匹配变量使用
- perl 正则命名捕获
- .NET正则表达式的命名捕获
- Python-正则零宽断言及命名捕获(类PHP)
- Python正则表达式--无捕获组和命名组
- java正则表达式学习笔记之命名捕获
- 前向否定界定符 python正则表达式不匹配某个字符串 以及无捕获组和命名组(转)
- perl 正则捕获 /(?<request_time>\d+(?:\.\d+)?)\s+/
- Java 正则表达式 - 命名捕获组
- [疯狂Java]正则表达式:捕获组、反向引用、捕获组命名
- grok 正则捕获(就是perl的正则捕获)
- grok 正则捕获(就是perl的正则捕获)
- grok 正则捕获(就是perl的正则捕获)
- JS 正则中的命名捕获分组
- JDK7 正则表达式 捕获组命名
- 用正则表达式判断邮箱命名是否合法
- posix和perl标准的正则表达式区别
- java 正则表达式 特殊构造(非捕获)理解 (1)
- python正则中的捕获组理解