0

Im looking for a regex that checks for:

a minimum of 2 numbers and

a minimum of 1 capital letter and

a minimum of 4 lowercase letters

And also checks for:

a maximum of 30 characters

I've been trying to make this but all my creations don't work :) You can leave the maximum out too if you can't do it, I could check it in another way.

2 Answers 2

2

I guess the order of those conditions is arbitrary. Therefore there is no need to do it using just 1 regexp. For each condition, you can have 1 regexp and then you can do a logical conjunction in your favorite language and it would be much more readable than one super-cool-ninja-regexp.

  • a minimum of 2 numbers

".*\d.*\d.*"

  • a minimum of 1 capital letter and

".*[A-Z].*"

  • a minimum of 4 lowercase letters

".*[a-z].*[a-z].*[a-z].*[a-z]"

  • a maximum of 30 characters

".{6,30}"

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

Comments

1
^(?=.*\d.*\d)(?=.*[A-Z])(?=.*[a-z].*[a-z].*[a-z].*[a-z]).{7,30}$ 

But if you want only the alphanumerics, then:

^(?=.*\d.*\d)(?=.*[A-Z])(?=.*[a-z].*[a-z].*[a-z].*[a-z])[a-zA-Z0-9]{7,30}$ 

(?=.*\d.*\d): at least two digits

(?=.*[A-Z]): one caps letter

(?=.*[a-z].*[a-z].*[a-z].*[a-z]): minimum four lowercase

[a-zA-Z0-9]{7,30}: length between 7-30

4 Comments

Thanks, it works like a beast! Only I changed {7,30} to {6,30} since the 4 lowercase letters may also be 3 lowercase letters and 1 uppercase ;)
No problem, either way it will do! :-)
Can you please explain how it works and in particular what ?= does? I am reading man perlre but I cannot put it together yet.
(?=.*[A-Z]) means it check to see whether there is any Capital letter in front. Similar for the other (?=). It says Look-Ahead in regex.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.