in reply to Re^3: Nonrepeating characters in an RE (simple)
in thread Nonrepeating characters in an RE
When the OP talks about "a template that might look like abcdefa", I interpret that to represent a pattern that words should follow, as in a substitution cypher: the first and seventh letters should be the same, all other letters should be different from those and from each other. Thus it should match "suitors" and "realtor", but not "bracken" or "suffers" or "straits" or "albania".
So the regexp translation becomes: for each letter in the template, if it has not been seen before introduce a new capture, and insist it doesn't match any of the previous captures; if it has been seen before, just match the appropriate previous capture:
sub template_to_regexp { my($template) = @_; my $seen_count; my %seen_at; my $regexp = ''; for my $i (0 .. length($template) - 1) { my $chr = substr($template, $i, 1); my $seen = $seen_at{$chr}; if (defined $seen) { $regexp .= "\\$seen"; next; } # else it's a new template character $regexp .= sprintf '(?!%s)', join '|', map "\\$_", 1 .. $seen_coun +t if $seen_count; $seen_at{$chr} = ++$seen_count; $regexp .= '(.)'; } return qr{$regexp}; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: Nonrepeating characters in an RE (simple)
by BillKSmith (Monsignor) on Aug 16, 2022 at 13:54 UTC | |
Re^5: Nonrepeating characters in an RE (simple)
by LanX (Saint) on Aug 16, 2022 at 12:58 UTC |