I have the regex below
^(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*W)[A-Za-z0-9W][x00-x7F]{8,}$
am expecting from the regex the below
- not less than 8 in length
- at least one capital letter
- at least one small letter
- at least one digit
- at least one non-alphanumeric symbol
- all characters should be Latin only (no extended char like Ş in Turkish or special letters in German like Ü and no Arabic or such languages )
what am facing is the regex does match many strings, but, as a string like 123aB!ВЕ it’s not matching. Another example is the string June!12 It does not match either, but it could be matched if extended to be 9 letters in length June!12sd ???
for example, Fth4&yusÖ should not match because of the letter Ö but when removed it should match, but, it is not matching unless it’s extended to be more than 9 characters.
any help is appreciated
2
Answers
It sounds like you’re just doing password validation, and regex is a tool you choose to use rather than a requirement. This sort of thing can be better (faster + easier to read) without regex.
Here’s a C# solution without using regex.
This works by processing each character one by one and updating some booleans based on if it passes a rule or not. Because you don’t have any "max number of …" rules, simply setting the boolean to true every time you have a matching character is fine.
It passes all your examples:
123aB!ВЕ
fails because the last two characters are Cyrillic.123aB!BE
passes – this is an altered version of your example to make the last two characters use Latin letters.June!12
fails as it less than 8 charactersJune!12sd
passesFth4&yusÖ
fails because of the last character.This is a good way to do your specs.
Note that white space is excluded as are other control codes.
Explained