zoukankan      html  css  js  c++  java
  • perl 正则命名捕获

    #!/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
    $second
    ";  # jack, rose 
    } 
    
    
    zjtest7-frontend:/root/perl# perl a2.pl 
    jack
    rose
    
    
    s  空格,和 [
    	
    f] 语法一样  
    s+ 和 [
    	
    f]+ 一样  
    S  非空格,和 [^
    	
    f] 语法一样  
    S+ 和 [^
    	
    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
    $second
    $third
    ";  # 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."
    ";};
    
    zjtest7-frontend:/root/perl# perl a3.pl 
    123.456
    

  • 相关阅读:
    MySQL 优化
    Log4j2 中format增加自定义的参数
    MySQL 索引
    Linux中top和free命令详解(转)
    JAVA面试题
    Servlet3.0的可插拔功能
    开放通用Api,总有你喜欢的
    Git常用命令
    支付宝无法回调或者回调后验签失败
    Promise
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350392.html
Copyright © 2011-2022 走看看