Nike
Nike

Reputation: 25

Single regex pattern for multiple criteria

Question is not duplicate with other SO questions because the answers are not matched with criteria listed, Please refer below.

Criteria to achieve with regex pattern for the input text is,

No Start and End Criteria.

Trying to achieve with below single regex,

^(
  (\w+\d+[@%\$]+)
     |
     (\d+[@%\$]+\w+)
        |
        ([@%\$]+\d+\w+)
             |
             (\w+[@%\$]+\d+)
)$

Problem is,

Support for at-least one uppercase is not working. I am pretty sure that it is not a good approach to build the regex pattern.

Please help me to achieve these criteria in single regex pattern.

Pass Criteria

Test@123
tesT@123
@123tesT
123@Test
TTTTeeeess@@@@$$$111112222
@@@@$$$1111TTT@@@$$esss

Fail Criteria

Test (No special character)
@123
123
@
T
test
test@123 (No Uppercase)
Test@123& ('&' not to be supported in the pattern)
@123test
@TTT123 (No Lowercase)

Upvotes: 2

Views: 103

Answers (2)

anubhava
anubhava

Reputation: 785146

You can just use this single lookahead based regex:

^(?=.*?[A-Z])(?=.*?[a-z])(?=\D*\d)(?=.*?[$@%])[\w$@%]+$

RegEx Demo

Upvotes: 3

AMDcze
AMDcze

Reputation: 526

Try it like this (changed \w to [a-zA-Z\d] since you probably don't want _ sign):

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)(?=.*?[@%$])[a-zA-Z\d@%$]+$

Demo: https://regex101.com/r/kS3cI4/2

Upvotes: 0

Related Questions