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.

Igor Strekha
  • 176
  • 3
  • 17
  • 2
    This looks like a JSON string. Better to use a json parser. – anubhava Apr 26 '21 at 07:18
  • 1
    Copy your json into any of the tool that generate a class for you https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string. And simply `var my_object = JsonConvert.DeserializeObject(jsonText)` and voila your object is populated with all your data. – Self Apr 26 '21 at 07:54
  • It will be `var pattern = $@"^(?=(?:[^A-Z]*[A-Z]){{{min_number_of_upper_value}}})(?=(?:\D*\d){{{min_num_of_numbers_value}}})";` – Wiktor Stribiżew Apr 26 '21 at 09:47
  • And why doing this whole logic in one Regex? "Always program so that you could give your successor your home address" – Klamsi Apr 26 '21 at 09:48

1 Answers1

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Does not work. I added 5 upper laters and 4 digitals https://regex101.com/r/YaCkLT/1 – Igor Strekha Apr 26 '21 at 13:03
  • @IgorStrekha See the correct test: https://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](https://ideone.com/j7wh44) generating `^(?=(?:[^A-Z]*[A-Z]){5})(?=(?:\D*\d){4}).*` correctly. – Wiktor Stribiżew Apr 26 '21 at 13:05
  • 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... – Igor Strekha Apr 26 '21 at 13:51
  • @IgorStrekha See [how to match special chars](https://stackoverflow.com/questions/32311081/check-for-special-characters-in-string/32311188#32311188), 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 `^`. – Wiktor Stribiżew Apr 27 '21 at 07:34