0

I am trying to find out a regex pattern to check if a password contains at least: one lowercase letter, one uppercase letter, one numeric digit, and one special character(Symbol)

I came across this regex: ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s) but I don't really understand well what each part is doing, If someone has a bit of free time I would be very thankful if you could explain to me what each part does.

Why u do dis
  • 418
  • 4
  • 15

3 Answers3

1

If you paste it into https://regex101.com/ you get this explanation. I suggest you use this site to experiment and understand. Also the explanation is formatted in a much better way

If you want to learn regex you need to start with something simpler

^ asserts position at start of a line Positive Lookahead (?=.*\d) Assert that the Regex below matches . matches any character (except for line terminators) * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) \d matches a digit (equivalent to [0-9]) Positive Lookahead (?=.*[a-z]) Assert that the Regex below matches . matches any character (except for line terminators) * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) Match a single character present in the list below [a-z] a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive) Positive Lookahead (?=.*[A-Z]) Assert that the Regex below matches . matches any character (except for line terminators) * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) Match a single character present in the list below [A-Z] A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive) Positive Lookahead (?=.*[^a-zA-Z0-9]) Assert that the Regex below matches . matches any character (except for line terminators) * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) Match a single character not present in the list below [^a-zA-Z0-9] a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive) A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive) 0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive) Negative Lookahead (?!.*\s) Assert that the Regex below does not match . matches any character (except for line terminators) * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) \s matches any whitespace character (equivalent to [\r\n\t\f\v ]) Global pattern flags g modifier: global. All matches (don't return after first match) m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
1

Your regex pattern contains a number of positive lookahead assertions, which are checking for the presence of various things in the input. Here is an explanation:

^                   from the start of the input
(?=.*\d)            assert one or more digits are present
(?=.*[a-z])         assert lowercase letters
(?=.*[A-Z])         assert uppercase letters
(?=.*[^a-zA-Z0-9])  assert one or more characters which are NOT alphanumeric
(?!.*\s)            assert one or more whitespace characters
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

You can google the regular expression cheat sheet to have an understanding of it. Here is one that I found useful, https://cheatography.com/davechild/cheat-sheets/regular-expressions/

You can also look at https://regex101.com/ and play around with regex.

Ravin Rau
  • 19
  • 2