Rethink your algorithm. It sounds like you're trying to do it bottom-up, making a fancy decision at each position for what should go into that position. What if instead you separated the problem into two pieces: the position in the password where the hint appears, and the remaining non-hint characters? Those two together can be processed to produce a unique password.

Regexps are more for going the other way around: given this password, separate it into the hint and other characters. Sure, you could use substitution to produce a password from a hint pattern:

$other = "x!4Y"; $password = $PATTERN; # $PATTERN is aHINTbcd $password =~ s/a/x/; # or $password =~ s/a/substr($other,0,1)/e; $password =~ s/b/!/; $password =~ s/c/4/; $password =~ s/d/Y/;
but the regular expressions aren't doing you any favors. tr/// would do it in one step:
$other = "x!4Y"; $password = $PATTERN; # $PATTERN is aHINTbcd $password =~ tr/abcd/$other/;
but that's still far more complicated than just tacking the pieces together:
$password = substr($other, 0, 1).$HINT.substr($other, 1, 3);
and less flexible than using join():
$password = join('', substr($other, 0, 1), $HINT, substr($other, 1, +3));
which can be easily parameterized to reposition the hint within the password.

Yes, regexps implicitly do looping, so it is possible to do the entire thing with a "single" (ir)regexp, but it would be a mess, and much harder to write than straightforward code. (I put "single" in quotes because the body of the regexp would contain at least one code block with multiple statements.)

More likely, you could generate every possible full-length password and use a regexp to filter out the ones containing the hint -- but that rather defeats the purpose of having a hint.


In reply to Re: Brute Force Algorithm with Perl reg exprs? by sfink
in thread Brute Force Algorithm with Perl reg exprs? by scotchfx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.