1

I am trying to figure out a regex to match a password that contains

  1. one upper case letter.
  2. one number
  3. one special character.
  4. and at least 4 characters of length

the regex that I wrote is

^((?=.*[0-9])(?=.*[A-Z])(?=.*[^A-Za-z0-9])){4,}

however it is not working, and I couldn't figure out why.

So please can someone tell me why this code is not working, where did I mess up, and how to correct this code.

5
  • 2
    Give some valid and invalid examples. Commented Mar 18, 2016 at 19:56
  • 1
    Like so: ^(?=.*[0-9])(?=.*[A-Z])(?=.*[^A-Za-z0-9]).{4,}$ Commented Mar 18, 2016 at 19:56
  • Your regex is not working because you quantified the group of lookaheads. You need to add the dot and the limiting quantifier to actually match and consume the string. Commented Mar 18, 2016 at 20:03
  • however it is not working, and I couldn't figure out why. - And yet you show advanced knowledge of look ahead assertions, and anchors, and other quantifiers. There are literally tens of thousands of identical questions (almost word for word) on this site. Commented Mar 18, 2016 at 21:52
  • Thanks to everyone for your feedback. noob. (adsd123@%A) This should work. as a valid password. Sebastian, Wiktor Thanks. sln Believe me, I studied for regex for the first time yesterday, and from what I understood my code should work, but it did not. I looked at some of the password questions here. but they do not tell you where you made your mistake, so that is why I had to ask. Commented Mar 18, 2016 at 22:28

2 Answers 2

1

Your regex can be rewritten as

^(
  (?=.*[0-9])
  (?=.*[A-Z])
  (?=.*[^A-Za-z0-9])
 ){4,}

As you see {4,} applies to group which doesn't let you match any character since look-around is zero-width, which effectively means "4 or more of nothing".

You need to add . before {4,} to let your regex handle "and at least 4 characters of length" point (rest is handled by look-around).
You can remove that capturing group since you don't really need it.

So try with something like

^(?=.*[0-9])(?=.*[A-Z])(?=.*[^A-Za-z0-9]).{4,}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. So what do you mean of "Since look-around is zero width" I did not get this part.
Look-around mechanisms like (?=...) (?<=...) don't consume part which will be matched by them which allows us to apply few tests in one place (we can test same part of string more than once). So since they don't move cursor forward after they perform test we say that they are zero-length/zero-width. More info at regular-expressions.info/lookaround.html and regular-expressions.info/zerolength.html
0

You could come up with sth. like:

^(?=.*[A-Z])(?=.*\d)(?=.*[!"§$%&/()=?`]).{4,}$

In multiline mode, see a demo on regex101.com.
This approach specifies the special characters directly (which could be extended, obviously).
From the following list only the bold ones would satisfy these criteria:

  • test
  • Test123!
  • StrongPassword34?
  • weakone
  • Tabaluga"12???

You can still enhance this expression by being more specific and requiring contrary pairs. Just to remind you, the dot-star (.*) brings you down the line and then backtracks eventually. This will almost always require more steps than to directly look for contrary pairs.
Consider the following expression:

^                    # bind the expression to the beginning of the string
(?=[^A-Z\n\r]*[A-Z]) # look ahead for sth. that is not A-Z, or newline and require one of A-Z
(?=[^\d\n\r]*\d)     # same construct for digits
(?=\w*[^\w\n\r])     # same construct for special chars (\w = _A-Za-z0-9)
.{4,}
$

You'll see a significant reduction in steps as the regex engine does not have to backtrack everytime.

6 Comments

Can you explain the third part of this regex ([!"§$%&/()=?`]. Thank you
One of these characters needs to be present in order for the regex to succeed. It is a character class.
my bad. I should get this easily. Thank you for your feedback.
@IsmaelAlsabea: No worries, glad to help. If the answer helps you, you might consider upvoting/accepting it though.
I did. but it does not show to public it says that my points should be more than 15.
|

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.