The TASK #2 of perl-weekly-challenge-061 was to split a given string into certain subparts. There were two solutions that (ab)use Perl's regular expression engine to get all matches for the leading part of a regular expression. Though being one of the authors, I'm not so sure about this approach. How smart is the engine allowed to be? Is there a way to guarantee that it actually tries all possibilities?
The section Embedded Code Execution Frequency in perlre says:
How non-accepting pathways and match failures affect the number of times a pattern is executed is specifically unspecified and may vary depending on what optimizations can be applied to the pattern and is likely to change from version to version.This is a rather clear statement, that the proposed solutions may fail in future versions of Perl. But does this hold in any case? See examples in this program:
#!/usr/bin/perl use strict; use warnings; my $match = qr[([ab]+)([ab]+)]; my $str = 'aba'; $str =~ /^ $match $ (?{ print "1: $1-$2\n" }) [c] /x; $str =~ /^ $match $ (?{ print "2: $1-$2\n" }) (?!) /x; $str =~ /^ $match $ (??{ print "3: $1-$2\n"; qr[(?!)] }) /x; __DATA__ 2: ab-a 2: a-ba 3: ab-a 3: a-ba
Explanations to the numbered samples:
A (??{CODE}) block is guaranteed to be executed, if the failing or success of a pathway containing this block solely depends on the returned subexpression.Could we even have a zero-width assertion like (?!?{CODE}) that always fails but must not be optimized away in the sense of the previous proposition?
I'd be glad to see your opinions.
BTW: What matches and what is matched? Is a regex matching a string or is a string matching a regex? I don't know.
Greetings,
-jo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: (Ab)using the Regex Engine
by tybalt89 (Monsignor) on May 25, 2020 at 22:00 UTC | |
by jo37 (Curate) on May 26, 2020 at 10:41 UTC | |
by vr (Curate) on May 26, 2020 at 15:51 UTC | |
by jo37 (Curate) on May 26, 2020 at 17:55 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |