For completeness, here we go.
But the need for use re 'eval'; surprised me, and might reduce the general usability.
use v5.12; use warnings; use re 'eval'; my $re = templ2regex("Mississippi"); for my $inp (qw/Mississippi Mossossoppo Miiiiiiippi/) { if ( my @matches = ( $inp =~ /$re/ ) ) { say "Match: $inp re: $re"; } } # Mississippi -> (.)(.)(.)\3\2\3\3\2(.)\4\2 sub templ2regex { my ($template) = @_; my ( $re, %grp, $cnt ); # not sure if that's better readable than before $re = join "", map { $grp{$_} // do { $grp{$_} = "\\" . ++$cnt; "(.)" } } split //, $template; $re .= '(?(?{not is_uniq( @{^CAPTURE} ) }) (*FAIL) )'; return $re; } # @_ elements all distinct? sub is_uniq { my %uniq; @uniq{@_} = (); return @_ == keys %uniq; }
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
In reply to Re^3: Nonrepeating characters in an RE
by LanX
in thread Nonrepeating characters in an RE
by BernieC
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |