you are in the right step with lookahead assertions.
Your requirements can be broken down to this :
- (?=.{8,})
- (?=.*\d)
- (?=.*[a-z])
- (?=.*[A-Z])
- (?=.*[!@#$%-])
- [\da-zA-Z!@#$%-]*
Putting it all together you end up with this :
foundMatch = Regex.IsMatch(subjectString, @"^(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%-])[\da-zA-Z!@#$%-]*$");
Which can in turn be explained by this :
"
^ # Assert position at the beginning of the string
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
{8,} # Between 8 and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\d # Match a single digit 0..9
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[A-Z] # Match a single character in the range between “A” and “Z”
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[!@#$%-] # Match a single character present in the list below
# One of the characters “!@#$”
# The character “%”
# The character “-”
)
[\da-zA-Z!@#$%-] # Match a single character present in the list below
# A single digit 0..9
# A character in the range between “a” and “z”
# A character in the range between “A” and “Z”
# One of the characters “!@#$”
# The character “%”
# The character “-”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
"