I could not seem to understand (?=.*?[A-Z]) this expression

regex

Solution

- `(?=` is the start of a look-ahead group — the question mark does not mean the same as a `?` elsewhere

- `.*?` is a non-greedy match against anything or nothing. The question-mark here also does not mean 'optional'.

- `[A-Z]` is a character set containing the upper case ASCII letters `A` through to `Z`.

- `)` is the end of the look-ahead group

So the net result is:

"Look ahead and see if, after maybe some characters, there is an upper case letter."

Your full expression, `^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$`, can be read as:

"Match if the string contains an upper case letter, and a lower case letter, and a digit, and a non-alphanumeric, and there are at least 8 characters in total."

Problem

I'm trying to learn a more advanced regular expressions for a password validator I'm working on because I think using regular expressions would be the best way out. I am using Java as my programming language So for my pattern people suggested this `(?=.*?[A-Z])` as to say "at least one upper case in the string". I have tried searching it at least but nothing seems to make it clear `?=.*?` how this part makes sure it at least there. here is the whole pattern `^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$` from what i understand - ? means optional and occurs once - = means well i don't know yet - . is a wildcard - [A-Z] is the range of uppercase letters from A-Z TLDR: So my question is how does this `(?=.*?[A-Z])` make it sure atleast one uppercase letter is included? Any in-depth explanation?

Original source

Related problems