0

Need regular expression to check the password policy

I have a model:

"password_settings": {
    "enable_password_max_length": false,
    "enable_password_min_length": false,
    "enable_min_number_of_upper": false,
    "enable_min_number_of_lower": false,
    "enable_min_number_of_numbers": false,
    "enable_min_number_of_special_characters": false,
    "password_max_length_value": 24,
    "password_min_length_value": 1,
    "min_number_of_upper_value": 1,
    "min_number_of_lower_value": 0,
    "min_num_of_numbers_value": 1,
    "min_number_of_special_characters_value": 1
},

And if some flags are "True" I need to create Regex dynamically.

for example:

enable_min_number_of_numbers= true
enable_min_number_of_upper = true
min_num_of_numbers_value= 3
min_number_of_upper_value = 2

Regex string will be

^(?=.*[A-Z]).{"min_number_of_upper_value",}(?=.*\d).{"min_num_of_numbers_value",}$

but it does not work for me.

What I need: Help me please to create Regex where I can disable or enable some parts of Regex and where I can set counts from my json.

Thank you.

4
  • 2
    This looks like a JSON string. Better to use a json parser. Commented Apr 26, 2021 at 7:18
  • 1
    Copy your json into any of the tool that generate a class for you stackoverflow.com/questions/21611674/…. And simply var my_object = JsonConvert.DeserializeObject<MyAutoGeneratedClass>(jsonText) and voila your object is populated with all your data. Commented Apr 26, 2021 at 7:54
  • It will be var pattern = $@"^(?=(?:[^A-Z]*[A-Z]){{{min_number_of_upper_value}}})(?=(?:\D*\d){{{min_num_of_numbers_value}}})"; Commented Apr 26, 2021 at 9:47
  • And why doing this whole logic in one Regex? "Always program so that you could give your successor your home address" Commented Apr 26, 2021 at 9:48

1 Answer 1

1

Given your min_number_of_upper_value contains the min threshold of upper ASCII letters allowed in the string and min_num_of_numbers_value defines the min amount of digits in the string, you can use

var pattern = $@"^(?=(?:[^A-Z]*[A-Z]){{{min_number_of_upper_value}}})(?=(?:\D*\d){{{min_num_of_numbers_value}}})";

Do not use $, this anchor requires the end of string. If your code requires a full string match, then use

var pattern = $@"^(?=(?:[^A-Z]*[A-Z]){{{min_number_of_upper_value}}})(?=(?:\D*\d){{{min_num_of_numbers_value}}}).*";

where you may further adjust the consuming (.*) pattern in case your requirements are more specific.

Sign up to request clarification or add additional context in comments.

4 Comments

Does not work. I added 5 upper laters and 4 digitals regex101.com/r/YaCkLT/1
@IgorStrekha See the correct test: regex101.com/r/M2nSxf/1. In an interpolated string literal, a single literal { char is defined with double {. Same goes for the } char. See the C# snippet generating ^(?=(?:[^A-Z]*[A-Z]){5})(?=(?:\D*\d){4}).* correctly.
thank you. But could me help more. (?:[^a-z]*[a-z]){5}) - this part for lower letters, (?=(?:[^A-Z]*[A-Z]){5}) - for Upper letters, (?=(?:\D*\d){4}) - for digits. What will be for special symbols? and What will be for minimum lengh and + maximum? Thank you Wiktor...
@IgorStrekha See how to match special chars, I cannot help here without detailed specs. As for total min,max length, just use (?=.{{{min},{max}}}$) or (?=(?s:.){{{min},{max}}}$) (to allow linebreaks) after ^.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.