简体   繁体   English

提高Perl Regex中最后一次匹配的性能

[英]Improve Performance of Last Occurrence Match in Perl Regex

I need to find the last occurrence of matches based on an array of acceptable of value. 我需要根据可接受的值数组找到匹配的最后一次出现。 Below is the source codes in Perl. 以下是Perl中的源代码。 The answer is Q because it is the last occurrence based on acceptable values of A, Q, I & J. 答案是Q,因为它是基于A,Q,I和J的可接受值的最后一次出现。

The challenge is how can I change my codes to make the regex faster. 挑战在于如何更改代码以使正则表达式更快。 It is currently a bottleneck because I have to run it millions times. 当前这是一个瓶颈,因为我必须运行数百万次。

my $input = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
my $regex = qr/(A|Q|I|J)/;

my @matches = $input =~ m/\b$regex\b/g;

print $matches[$#matches];

I would like to see new codes that improves the query speed but still can find the Q match. 我希望看到可以提高查询速度的新代码,但仍然可以找到Q匹配项。

You can find the last match by simply adding a .* before the matching pattern. 您只需在匹配模式之前添加.*即可找到最后一个匹配项。

Like this 像这样

my $input = "APPLE B C D E F G H INDIGO JACKAL K L M N O P QUIVER R S T U V W X Y Z";
my $regex = qr/APPLE|QUIVER|INDIGO|JACKAL/;
my ($last) = $input =~ /.*\b($regex)\b/;
print $last, "\n";

output 输出

QUIVER

Use \\K to discard the previously matched characters from printing at the final. 使用\\K放弃最后匹配的先前字符。

my $input = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
my $regex = qr/.*\K\b[AQIJ]\b/;
if ($input =~ m/$regex/) {
print $&."\n";
}

Use capturing group. 使用捕获组。

my $input = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
my $regex = qr/.*\b([AQIJ])\b/;
if ($input =~ m/$regex/) {
print $1."\n";
}

Update: 更新:

my $input = "Apple Orange Mango Apple";
my $regex = qr/.*\K\b(?:Apple|Range|Mango)\b/;
if ($input =~ m/$regex/) {
print $&."\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM