perl 遇到的贪婪匹配的问题

api:/root# cat 1.log | grep AAAAA
10.171.246.184 - - [14/May/2015:16:30:29 +0800] "GET /web/noauth?method=%2FproductAAAAAA?
api:/root# perl sum.pl 
$var1 is GET /web/loginInfo?
$var1 is GET /web/loginInfo?
$var1 is GET /web/loginInfo?
$var1 is GET /web/loginInfo?
$var1 is GET /business/dispatch_get.do?
$var1 is GET /web/noauth?method=%2FproductAAAAAA?
$var1 is GET /web/loginInfo?
$var1 is GET /web/noauth?
$var1 is GET /business/dispatch_get.do?
$var1 is GET /web/noauth?

use HTTP::Date qw(time2iso str2time time2iso time2isoz);
my $CurrTime = (split / /,time2iso(time()))[0];
my %log;
#if ( $#ARGV < 1 ){  
#        print "please input your date!
";
#                exit(-1);  
#                    } 
#                    $date= $ARGV[0];  
#                    $ip_addr = $ARGV[1];
#统计每个IP的访问次数
                   #glob函数 每次返回符合条件的文件名
                    @file = glob "./1.log";
                   foreach $file (@file){
#                   print "------file is $file-----------
";
                    open (LOG ,"<","$file");
                    while (<LOG>) {
                    chomp;
                    ##正则条件匹配
                    if ($_ =~ /(d{1,3}.d{1,3}.d{1,3}.d{1,3})/){
                    #print "$1 is $1
";
                    $var= $1;
                    $log{$var}++;
                    }
                    if ($_ =~ /.*]*(GET.*?).*/){
                    $var1 = $1;
                   print "$var1 is $var1
";
                    $log1{$var1}++;
                    }
}};
                   while(my($ip, $times) = each %log) {
                   print "$ip 访问次数是 $times
";
};
                   while(my($html, $times) = each %log1) {
                   print "$html 访问次数是  $times
";
};




此时会贪婪匹配到 [14/May/2015:16:30:29 +0800] "GET /web/noauth?method=%2FproductAAAAAA?

-------------------------------------------------------------------------------------------------------
my %log;
#if ( $#ARGV < 1 ){  
#        print "please input your date!
";
#                exit(-1);  
#                    } 
#                    $date= $ARGV[0];  
#                    $ip_addr = $ARGV[1];
#统计每个IP的访问次数
                   #glob函数 每次返回符合条件的文件名
                    @file = glob "./1.log";
                   foreach $file (@file){
#                   print "------file is $file-----------
";
                    open (LOG ,"<","$file");
                    while (<LOG>) {
                    chomp;
                    ##正则条件匹配
                    if ($_ =~ /(d{1,3}.d{1,3}.d{1,3}.d{1,3})/){
                    #print "$1 is $1
";
                    $var= $1;
                    $log{$var}++;
                    }
                    if ($_ =~ /.*]*(GET.*??).*/){
                    $var1 = $1;
                   print "$var1 is $var1
";
                    $log1{$var1}++;
                    }
}};
                   while(my($ip, $times) = each %log) {
                   print "$ip 访问次数是 $times
";
};
                   while(my($html, $times) = each %log1) {
                   print "$html 访问次数是  $times
";
};

取消贪婪匹配: 此时只匹配到/web/noauth?

api:/root# perl sum.pl 
$var1 is GET /web/loginInfo?
$var1 is GET /web/loginInfo?
$var1 is GET /web/loginInfo?
$var1 is GET /web/loginInfo?
$var1 is GET /business/dispatch_get.do?
$var1 is GET /web/noauth?



原文地址:https://www.cnblogs.com/hzcya1995/p/13351676.html