0

This question is complication of problem in Regex: I want this AND that AND that… in any order. The most up-voted solution of this question is:

^(?=.*one)(?=.*two)(?=.*three).*$

Now what if I need to check not only characters set, but also quantity of characters of certain kind? For example:

  • At least two uppercase Latin letters
  • At least two lowercase Latin letters
  • At least two digits
  • At least two of specified special characters

... is any order.

I have no idea where from to begin because I don't understand what ?=. doing in our case.

Takesi Tokugawa YD
  • 49
  • 2
  • 16
  • 53
  • _“I have no idea where from to begin because I don't understand what `?=.` doing in our case”_ — See [Reference - What does this regex mean?](https://stackoverflow.com/q/22937618/4642212) and use regex debuggers like [RegEx101](https://regex101.com/). – Sebastian Simon Jul 03 '20 at 03:26
  • 2
    Just as a hint, to assert that two or more uppercase letters occurs, you could add another positive lookahead: `(?=.*[A-Z].*[A-Z])` ... and do something similar for the other requirements. – Tim Biegeleisen Jul 03 '20 at 03:32
  • @user4642212, done. My language is Type/JavaScript. – Takesi Tokugawa YD Jul 03 '20 at 04:16
  • @TimBiegeleisen thank you for the hint. Would you please how is will be for three and more case? It's unobvious for me how to scale your solution from 2 to n cases. – Takesi Tokugawa YD Jul 03 '20 at 04:18

1 Answers1

2

One option to handle the additional four requirements would be to just add additional positive lookahead assertions:

^
    (?=.*one)                                             match "one" (in any order)
    (?=.*two)                                             match "two"
    (?=.*three)                                           match "three"
    (?=[^A-Z]*[A-Z][^A-Z]*[A-Z])                          two uppercase letters
    (?=[^a-z]*[a-z][^a-z]*[a-z])                          two lowercase letters
    (?=\D*\d\D*\d)                                        two digits
    (?=[A-Za-z0-9]*[^A-Za-z0-9][A-Za-z0-9]*[^A-Za-z0-9])  two symbols (two non alphanumeric)
    .*                                                    consume the input
$

Demo

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263