No need to use a regex.

If the difference in length between both passwords is 3 or more, you already have your 3 character difference.

Otherwise check the first character of the shorter string against the first character of the longer string, do the same with the second, third, ... characters, adding one to the difference in length between two passwords: as soon as your sum is three, the new password is OK.

If you run out of characters and still the sum is less than three, the new password is wrong.

However, if you simply add one new character to the beginning of the old password to make a new password, this algoritm will say this is OK and that is perhaps not what you are looking for.

If that is indeed the case, do the following:

  1. tally the different characters of each password in a hash for each password (the key being the character, the value being the number of times this character appears in the password).
  2. Then take the absolute difference between the value of each key in the hash, compared to the same key in the other hash. (you have to make certain that both hashes have the same keys, so you may have to add keys with value zero for characters which appear in one password but not in the other).
  3. Sum these absolute differences. If it is less than three, the new password is not OK, otherwise it is.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law


In reply to Re: regex in password changing by CountZero
in thread regex in password changing by jcpunk

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.