in reply to Nonrepeating characters in an RE

Since you have a template and talk about generating a pattern,

my %captures; my $pat = join "", map { if ( $captures{ $_ } ) { "\\$captures{$_}" } else { my $capture = 1 + keys( %captures ); $captures{ $_ } = $capture; if ( $capture == 1 ) { "(.)" } else { "(?!".( join "|", map { "\\$_" } 1 .. $capture-1 ).")(.)" } } } split //, $template; my $re = qr/^$pat\z/s;

For

my $template = "abcdefa";

this generates

^ (.) (?!\1)(.) (?!\1|\2)(.) (?!\1|\2|\3)(.) (?!\1|\2|\3|\4)(.) (?!\1|\2|\3|\4|\5)(.) \1 \z

(Line breaks added for readability.)

Replies are listed 'Best First'.
Re^2: Nonrepeating characters in an RE
by ikegami (Patriarch) on Aug 17, 2022 at 06:41 UTC

    This might be faster:

    ^ (.) (?: (?: \1 ) (*COMMIT) (*FAIL) | (.) ) (?: (?: \1 | \2 ) (*COMMIT) (*FAIL) | (.) ) (?: (?: \1 | \2 | \3 ) (*COMMIT) (*FAIL) | (.) ) (?: (?: \1 | \2 | \3 | \4 ) (*COMMIT) (*FAIL) | (.) ) (?: (?: \1 | \2 | \3 | \4 | \5 ) (*COMMIT) (*FAIL) | (.) ) \1 \z