0

I need a Regex to search in text files for typical password patterns, such as

A word that contains lower case, upper case letters, and digits, and is 6-30 characters long in an arbitrary text, such as the following.

This is some sample text containing an email address of michael.knight@knightrider2000.com 
and some cryptic secret like am2Nals2kdP5 and even more text.

I'd like to match am2Nals2kdP5, but nothing else in that example.

I tried \s[a-zA-Z0-9]{6,30}\s, but this matches other word like containing, too. I understand that this is, because [...] matches to any of the characters, instead of all of them. However I cannot find the proper syntax.

vrdse
  • 2,899
  • 10
  • 20
  • Possible duplicate of [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) – Biffen Feb 13 '18 at 12:37
  • `[…]` matches *any* of the enclosed characters, not *all*. – Biffen Feb 13 '18 at 12:39
  • Thanks, I can understand why `[...]` is not working. I adjusted my initial post accordingly. But how does this lead me to a solution do my problem? – vrdse Feb 13 '18 at 13:53

1 Answers1

1

Your regex \s[a-zA-Z0-9]{6,30}\s would match any whitespace character \s, any in these ranges [a-zA-Z0-9] with {6,30} for the repetition followed by any whitespace character \s.

This would match for example aaaaaa or 1Aa2bbzzzzzzz

You could use positive lookaheads (?= to assert that what follows contains a lowercase character, an uppercase character and a digit.

For example:

(?=[A-Z\d]*[a-z])(?=[a-z\d]*[A-Z])(?=[a-zA-Z]*[\d])[a-zA-Z\d]{6,30}

Note that this does not take the whitespaces before and after into account.

To do this, you could add a positive lookbehind (?<=^|\s) at the beginning to assert that what is before is the beginning of the string or any whitespace character and add a positive lookahead (?=$|\s) to assert that what follows is the end of the string or any whitespace character.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70