Regular Expression 是Perl 语言的强项,虽然现在其他的语言都支持Regular Expression,其差别还是有的。这里算法关键是:
使用预编译Reguler Expression:
$_ = qr/$_/;
使用grep函数:
@matches = grep( { $string =~ /$_/ } @patterns);
测试用的是1.4Ghz Dell Laptop PC, Windows XP with Perl 5.8.
这里是测试的Perl Code:
#!/usr/bin/perl
use strict;
use POSIX qw(strftime);
my ($string, @strings,$pattern, @subpatterns, @patterns, @matches, $totalmatches);
#输入字符串和Pattern.
$string = "1234567890123456789012345678901234567890"; # 40个字符
#最后一个不匹配!
@subpatterns = ( "*123456789-*", "1234567890----------1234567890----*",
"----------1234567890------*--1234567890", "----------1235*--234567890-");
# 转化到Perl Patterns,并预编译patterns。
@subpatterns = map { $_=~s/\*/.*/g; $_=~ s/-/./g; $_ = qr/$_/; $_; } @subpatterns;
# 产生测试数据 : 2000个Pattern,500个字符串
push (@patterns, @subpatterns) foreach (1..500); # 2000 patterns
push (@strings, $string) foreach (1..500); # 500 string arrays
print "Total patterns: ", scalar(@patterns),
", Total number of strings: ", scalar(@strings),
", Total matches to be tested: ", scalar(@patterns) * scalar(@strings), "\n";
#进行匹配测试。
print "Start match at ", strftime("%Y/%m/%d,%H:%M:%S", localtime()), "\n";
$totalmatches =0;
foreach $string (@strings)
{ @matches = grep( { $string =~ /$_/ } @patterns);
$totalmatches += scalar(@matches);
}
print "Stop match at ", strftime("%Y/%m/%d,%H:%M:%S", localtime()), "\n",
"Total matched: $totalmatches.\n";
这里是测试的输出结果:
Total patterns: 2000, Total number of strings: 500, Total matches to be tested: 1000000
Start match at 2006/03/13,09:27:24
Stop match at 2006/03/13,09:27:29
Total matched: 750000.