Special way of forming regex?

regex

Solution

I can explain what the parts of the regex do, but in general I find this quite odd:

^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$

Basically what you said is true - there is no other magic in the regex.

`^.*` - match the beginning of the line and 0+ characters then ensure that

The following just assert - none of them matches/captures anything. It's called the positive lookahead if you want to look it up. if all of them evaluate to true, the last part of the regex will do the rest:

`(?=.{10,})` - from where the first matching stops (could be after the beginning of the line) there is a string of 10+ chars (any chars)

`(?=.*\d)` - and there is at least one digit in the whole string ahead

`(?=.*[a-z])` - and a lower case letter

`(?=.*[A-Z])` - and an upper case letter

If all that is true, then:

`.*$` - match everything till the end of the line

Note: if any of the asserts fail, nothing will be matched.

To your edit

I don't think so - it's not the same to say that there is an upper and lower case letter and a digit somewhere in the string, and to say that the string consists of 10+ characters of which all are either digits or letters (upper or lower case) or both. Your regex would match a string that consists of only digits as well as only letters or a mix of both - the original regex ensures that each of these classes is represented at least once. It seems that someone might have used it to validate a user password or something like that.

Problem

I've come across this regex and I was wondering how this is used: ``` ^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$ ``` I want to know what the individual section of the regex mean, not only what the regex in its whole does. With the knowledge of regex's I have, I think it matches for any input (at least 10 chars long) that matches a digit (0-9), lowercase and uppercase letters, but I need confirmation if this is correct? Edit I also don't know what it is meant to validate, but looking at what I think it does, is it right that the regex can be simplified to: ``` [\d|[a-zA-Z]]{10,} ``` Edit 2 I've noticed my replacement regex doesn't make sure I have at least one of every requirements (at least a digit, upcase and lowcase letter). Any way to adjust it so the regex does that as well, or is that only possible with the original regex?

Original source