简体   繁体   中英

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. The answer is Q because it is the last occurrence based on acceptable values of 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.

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.

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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