Building a regex to match passwords stored in plain text.
8-15 characters, must contain at least:
[A-Z]
[a-z]
\d
[!@#\$%\^&\*]
The problem I have is when the password is inline with other text or spaces after, it doesn't return a match. When it's on its own without spaces it matches.
Example:
This is a Testing!23 surrounded by other text.
Testing!23
(?=.{8,15})(?=.*[!@#\$%\^&\*])(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*
You want to find all non-whitespace chunks matching the conditions you outlined.
Use
(?<!\S)(?=\S{8,15}(?!\S))(?=[^!@#$%^&*\s]*[!@#$%^&*])(?=[^\s\d]*\d)(?=[^\sa-z]*[a-z])(?=[^\sA-Z]*[A-Z])\S+
See the regex demo
Details
(?<!\S)
- a whitespace or start of string should be right before the current position(?=\S{8,15}(?!\S))
- right after the current position, there must be 8 to 15 non-whitespace chars followed with either whitespace or end of string(?=[^!@#$%^&*\s]*[!@#$%^&*])
- there must be a char from the [!@#$%^&*]
set after zero or more non-whitespace chars outside of the set(?=[^\s\d]*\d)
- there must be a digit after 0+ non-whitespace and non-digit chars(?=[^\sa-z]*[a-z])
- 1 lowercase letter must appear after 0+ chars other than whitespace and lowercase letters(?=[^\sA-Z]*[A-Z])
- 1 uppercase letter must appear after 0+ chars other than whitespace and uppercase letters\S+
- all checks are over, and if they succeed, match and consume 1+ non-whitespace chars (finally).