How can I write a regex that can find passwords in texts and binary data?
07:23 16 Apr 2026

I want to write a regular expression that detects passwords in texts and binary data, so that I can flag files that contain potential secrets.

The rule for a password should be something like:

The string should contain:

  1. at least 10 characters

  2. at least one lower case letter

  3. at least one higher case letter

  4. at least one numeric character

  5. at least one punctuation character

I've tried writing it in PCRE flavour of regex (tested with PHP in https://regex101.com) with lookbehinds and lookaheads, but have failed in general. Is there any way out there to achieve the goal?

My current regex is:

(?

that seems to work for strings, but not on binary data where the password is not surrounded by whitespace characters

regex