Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I write regex expression to check a password for some conditions?

I am working on a requirement wherein I have to create a strong password. The following condition has to be met:
-> It should be a combination of Cap Letter, Small Letter, Number and Special character

Can I write a regex expression for this? If yes how ?

like image 510
Gurucharan Balakuntla Maheshku Avatar asked Dec 21 '22 16:12

Gurucharan Balakuntla Maheshku


1 Answers

For example, to validate a password of at least 8 characters:

if (Regex.IsMatch(subjectString, 
    @"^               # Start of string
    (?=.*\p{Lu})      # Assert at least one uppercase letter
    (?=.*\p{Ll})      # Assert at least one lowercase letter
    (?=.*\d)          # Assert at least one digit
    (?=.*[^\p{L}\d])  # Assert at least one other character
    .{8,}             # Match at least 8 characters
    $                 # End of string",             
    RegexOptions.IgnorePatternWhitespace))
like image 97
Tim Pietzcker Avatar answered Dec 29 '22 01:12

Tim Pietzcker