This code is similar to
hugo's, but with a couple key differences. First, it matches anything that isn't one of the backreferences (instead of specifically
\w), although you could change that behavior very easily. Second, it only transforms CAPITAL letters into the complex pattern, leaving lowercase letters alone. This allows you to turn "aTTeMPT" into a regex that matches words with the pattern of the word "attempt", such as "asserts" and "assents". It doesn't limit the capital letters to match letters that are shown in lowercase, but again, that's something you can add fairly easily.
sub make_pattern {
my $w = shift;
my %seen;
$w =~ s{([A-Z])}{
if ($seen{$1}) { "\\$seen{$1}" }
else {
$seen{$1} = 1 + keys %seen;
"(" . join("", map "(?!\\$_)", 1 .. ($seen{$1} - 1)) . ".)"
}
}ge;
return qr/^$w$/si;
}