1
votes

I want to create an expression for password as below:

Regex for passwords that must contain 8 characters, start with 2 lower or uppercase letters, contain one special character * and a 5-digit number.

E.g.: az*12345

  • It must be start with 2 characters;
  • Contain only single *;
  • End with 5 digits.

I have tried it with this pattern: (?=(.*[^a-zA-Z]){2})(?=.*[*]{1})(?=(.*\d){5}).{8}$

However, it yields almost the same results as a regex above. It starts with any character but I want the exact above mentioned pattern. I know I am close to it. Please suggest me what I should do.

1
Must the special character be the asterisk (*)? Wouldn't ^[a-zA-Z]{2}\*\d{5}$ be sufficient then?Paul
If you don't want it to start with any character, don't make it start with .*. That means any character zero or more times.Nick
thank paul.. fantastic.. it works..Paddy
thanks nick.. thats the solutionPaddy

1 Answers

2
votes

If you wish to match just [2-letters]+[*]+[5-digits] pattern, here is what you are looking for:

^[a-zA-Z]{2}\*[0-9]{5}$.