0

I couldn't make few regex patterns to work so i googled most of them and have encountered issues in the process. I mostly understand rules in play while building regex patterns but don't understand how to create regex that checks if in string is at least 1 certain type of character. This i found online:

'/^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})$/'

What i cant understand is this:

  • what this is for ?=.* does it like mean something together or just separate attributes
  • why is there . between curly brackets and normal ones ).{ as far as i know its any character, unless it means any of previously mentioned but what makes it to work that way
  • the last problem is that regex works the way that it actually checks if demanded characters are provided but also allow to type something like ąó or "(^! when i look at this i don't see anywhere that mentioned characters are allowed

As you already figured it out im trying to build password validation regex and struggling with this.

EDIT

If its possible could someone present alteration to this regex so it will not mean

  • require one of digit, lower case, upper case, special char of provided list and than whatever else to fill blanks

so it would instead mean

  • require one of digit, lower case, upper case, special char of provided list and fill blanks with characters of mentioned rules, so you can continue typing digits letters and special chars from provided list but not some special things like !óż
5
  • Are you new to regex or is the regex too messy to understand? Commented Jul 7, 2014 at 13:13
  • author of tutorial stated that this regex doesnt allow characters like this *!ó to be used, yet i can use them so im asking where is the problem, i started study regexes yesterday so hmm i might not know everything just yet Commented Jul 7, 2014 at 13:15
  • 1
    That tutorial is obviously wrong. Avoid it, use Regular-Expressions.info if you want to learn regexes. Commented Jul 7, 2014 at 13:23
  • can you then tell how to alter regex so it will mean 1 or more of mentioned characters beacuse this (\d+) or this (\d){1,} doesnt work at all Commented Jul 7, 2014 at 13:28
  • i did that, i guess i just need to replace that dot . to something else but dont know what should i place there Commented Jul 7, 2014 at 13:36

2 Answers 2

1

(?=...) is a positive lookahead assertion. It asserts that the enclosed regex could match at the current position without actually performing the match. So for example, (?=.*\d) means "check to see if it's possible to match any number of characters, followed by a digit" which translates to "check if there is at least one digit somewhere ahead in the string".

Breaking the regex down, this means:

^             # Start of string
(             # Match and capture in group 1:
 (?=.*\d)     # Assert that there is at least one digit in the string
 (?=.*[a-z])  # Assert that there is at least one lowercase letter in the string
 (?=.*[A-Z])  # Assert that there is at least one uppercase letter in the string
 (?=.*[@#$%]) # Assert that there is at least one of the characters @#$%
 .{6,20}      # Match 6-20 characters (any character except newlines)
)             # End of group
$             # End of string

The capturing group is wholly unnecessary, by the way.

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

3 Comments

And the rest of the regex?
.{6,20} "dot" in this part cannot mean any character as you want password to contain specificaly mentioned characters otherwise whats the point of creating sets of conditions
@user2668182: I was explaining what the regex is doing. If you want it to behave differently, you'd have to be more explicit (describing which characters you want to allow or forbid instead of using the dot to match any character). But why would anyone restrict the legal characters for a password? That would only make them unnecessarily unsafe.
0
/^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})$/

^ assert position at start of the string

1st Capturing group ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})

(?=.*\d) Positive Lookahead - Assert that the regex below can be matched

Any amount of any characters, followed by a digit. In other words, string must contain a digit.

(?=.*[a-z]) Positive Lookahead - Assert that the regex below can be matched

Any amount of any characters, followed by a single character in the range between a and z (case sensitive). In other words, string must contain a lowercase character.

(?=.*[A-Z]) Positive Lookahead - Assert that the regex below can be matched

Any amount of any characters, followed by a single character in the range between A and Z (case sensitive). In other words, string must contain an uppercase character.

(?=.*[@#$%]) Positive Lookahead - Assert that the regex below can be matched

Any amount of any characters, followed by a single character in the list @#$% literally. In other words, string must contain @#$%.

.{6,20} matches any character (except newline) between 6 and 20 times.

$ assert position at end of the string


Referenced by:

regex101


EDIT: Change . to [^__blacklisted_characters_here__]

2 Comments

well i could done that from the beginning but i dont feel like typing entire utf-8 library in here, i rather say what can be used, not what cant
@user2668182 "i dont feel like typing entire utf-8 library in here" What do you want to do then? This is getting opinionated. Using a list here is the optimal solution. There's \w and \W and ASCII character groups if you need a refresher.

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.