0

我正在尝试将这个“ServerLog(07-05-2013@22-07)”变成一个正则表达式。

my $filename = q{ServerLog((\d+)-(\d+)-(\d+)@(\d+)-(\d+))};

这就是我所拥有的,但它不起作用。有什么建议么?

编辑:这行得通吗?

my $filename = q{ServerLog\((\d+)-(\d+)-(\d+)@(\d+)-(\d+)\)};
my $log = "<D:\\ServerTools\\Logs\\$filename";
my $ref = tie *FH,"File::Tail",(name=>$log);
4

2 回答 2

5

您需要使用 a 转义第一个和最后一个括号,\因为您希望它们与字符串中的实际括号匹配:

/ServerLog\((\d+)-(\d+)-(\d+)@(\d+)-(\d+)\)/

像这样:

my $string = 'ServerLog(07-05-2013@22-07)';

if( $string =~ /ServerLog\((\d+)-(\d+)-(\d+)@(\d+)-(\d+)\)/) {
  print "matches";
}
于 2013-07-06T02:41:51.567 回答
0

您应该使用 /x 开关分隔复杂的正则表达式:

my $server_log_qr = qr{
        ServerLog
        \(
        (\d{2})     # capture month
        \-
        (\d{2})     # capture day
        \-
        (\d{4})     # capture year
        \@
        (\d{2})     # capture hour
        \-
        (\d{2})     # capture minute
        \)
    }msx;

my $example = 'ServerLog(07-05-2013@22-07)';
print "$example\n";

my ( $month, $day, $year, $hour, $minute ) = $example =~ $server_log_qr;
print "month  = $month\n";
print "day    = $day\n";
print "year   = $year\n";
print "hour   = $hour\n";
print "minute = $minute\n";
于 2013-07-06T12:10:03.813 回答