0

I want to understand what regular expression means

(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$

Below is what I understand the expression to me. Can you confirm or explain?

(?=^.{8,}$) string should be 8 character or more

((?=.*\d)|(?=.*\W+)) string should consist of one number or one special character. What is the plus after W?

(?![.\n]) Not sure what this part means.

(?=.*[A-Z])(?=.*[a-z]).*$ My understanding is that $ means end of expression, so it's kinda confusing why there's a dot and an asterisk before it. Also why is there only one $? Shouldn't it have two: one $ after .*[A-Z] and one after .*[a-z]? To say that this section is supposed to make sure that user typed one small and one capital letter?

I am using this code in html form for practice and it's working fine.

All together this regular should achieve this and it's doing it UpperCase, LowerCase, Number/SpecialChar and min 8 Chars

Edit: regex101.com i am also trying to understand on this side as @ymonad said in comment

xgord
  • 4,606
  • 6
  • 30
  • 51
user2860957
  • 456
  • 1
  • 7
  • 18

1 Answers1

1

Regex:

(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$

Explanation:

  • (?=^.{8,}$) - Positive Lookahead to validate that the test string has atleast 8 characters(except a new-line character) between the start and the end of string
  • (?=.*\d) - Positive lookahead to validate that the test string contains a digit
  • | - OR
  • (?=.*\W+) - Positive lookahead to validate that the input string has atleast 1 or more non-Word characters which DO NOT fall in this range [a-zA-Z0-9_]
  • (?![.\n]) - a Negative lookahead to validate that the input string does not have a newline character \n or a dot . at the current position
  • (?=.*[A-Z]) - Positive lookahead to validate that the input string has an Upper case letter [A-Z]
  • (?=.*[a-z]) - Positive lookahead to validate that the input string has a lower case letter [a-z]
  • .* - Until this point we were just validating our input string against various rules. Now, using .* we are matching 0+ occurrences of any character except a new line character
  • $ - asserts the end of the string

Also, as pointed out in the comments, THIS SITE can be a good start.

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
  • thanks for the answer do i really need this section `(?![.\n])` i am getting the impresssion that i dont – user2860957 Oct 19 '17 at 02:51
  • @user2860957 It depends on what you're actually trying to validate. I'm not sure why you'd need to check for newlines, but you may need to disallow `.` – Edward Minnix Oct 19 '17 at 02:53
  • Actually, it depends upon your requirements. This part is just validating that the current position should not be followed by a `.` or a `\n` i.e, newline character. I am assuming that if your current position is at the start of the string, it means that the 1st charcter should not be a `.` or a newline character. – Gurmanjot Singh Oct 19 '17 at 02:54
  • thanks nah i dont need this line than, as i got this regex online, and i was trying to understand it, in this portion was driving me crazy, and my requirment was `UpperCase, LowerCase, Number/SpecialChar and min 8 Chars` and @EdwardMinnix as well. – user2860957 Oct 19 '17 at 02:56