2

Hey guys I'm new to regex and I want to create 1 single regex that matches with texts that are

  • 5-30 letters long,
  • have no special characters,
  • have at least 4 capital letters,
  • have at least 2 lowercase letters and
  • at least 1 number.

Examples: should match

  1. https://stackoverflow.com/Abcde3FGhiDE/Zyx23 should match: Abcde3FGhiDE
  2. |a|b|c|AbcdEFGH123|456Ac should match: AbcdEFG123
  3. P A Abcde3FGhiDE Z H should match: Abcde3FGhiDE
  4. ZZ123!Abcde3FGhiDE!123 should match: Abcde3FGhiDE

Examples: no match

  1. <HeLLoWoRlD"123
  2. |A|b|c|D|E|F|1|
  3. NULLLLLLLLLLLLLLLLLLLLLLllll 1
  4. IAMoverTHELIMITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT3

Please make it as concise as you can. My attempt (demo):

(?=(?:\d*[A-Za-z]))(?=(?:\S*?[A-Z]){4,}\S*?$)(?=(?:[a-zA-Z]*\d))[A-Za-z0-9]{5,30}
AmigoJack
  • 5,234
  • 1
  • 15
  • 31
  • 1
    This site is not a code-writing service. We can help you with *specific* questions about *your* code, ideally accompanied by an [MCVE](https://stackoverflow.com/help/mcve). This looks very much like homework or some coding challenge - you'll learn more by solving it yourself. – Bohemian Aug 16 '20 at 23:26
  • Sorry, I'll provide what I have – Bob Johnson Aug 16 '20 at 23:27
  • Would that suffice? – Bob Johnson Aug 16 '20 at 23:39
  • 1
    Not bad. Can you also describe how it doesn't work? If not exactly, indicate the test cases it works correctly with and those it doesn't work correctly with? – Bohemian Aug 16 '20 at 23:51
  • https://regex101.com/r/w9BMgW/1 if thats what you were asking for. – Bob Johnson Aug 17 '20 at 00:03
  • Does this answer your question? [Regex to validate password strength](https://stackoverflow.com/questions/5142103/regex-to-validate-password-strength) – jdaz Aug 17 '20 at 00:25
  • This question is very similar to https://stackoverflow.com/q/63440383/4299358 – AmigoJack Aug 17 '20 at 01:16
  • The link that AmigoJack posted contains a regex that matches all but 1 sequences that I have provided, that being 123, I have tried inserting the regex that is (?=.*\d) into it, and that didn't seem to work either. – Bob Johnson Aug 17 '20 at 02:12
  • To clarify, the regex in the given link was \b(?=(?:[a-z\d]*[A-Z]){3})(?=(?:[A-Z\d]*[a-z]){2})[a-zA-Z\d]{10,20}\b and what I'm trying to accomplish is to at least has 1 number for it to match, and that regex seems to match 123 – Bob Johnson Aug 17 '20 at 02:13
  • Here's the proof if it helps making it any easier https://regex101.com/r/w9BMgW/3 – Bob Johnson Aug 17 '20 at 02:17
  • And unfortunately, no, jdaz, none of the regex provided in that link worked. – Bob Johnson Aug 17 '20 at 02:20

2 Answers2

2

You might use

\b(?=(?:[a-z0-9]*[A-Z]){4})(?=(?:[A-Z0-9]*[a-z]){2})(?=[a-zA-Z]*[0-9])[A-Za-z0-9]{5,30}\b

Explanation

  • \b Word boundary
  • (?=(?:[a-z0-9]*[A-Z]){4}) Assert 4 uppercase chars A-Z
  • (?=(?:[A-Z0-9]*[a-z]){2}) Assert 2 lowercase chars a-z
  • (?=[a-zA-Z]*[0-9]) Assert a digits
  • [A-Za-z0-9]{5,30} Match any of the listed 5 - 30 times
  • \b Word boundary

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

5-30 letters long, has at least 4 capital letters, 2 lowercase and at least 1 number

This mignt not be the cleanest but:

/^(?=[a-ZA-Z\d]{5,30}$)(?=(.*[A-Z]){4})(?=(.*[a-z]){2}).*\d/
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • This does not satisfy the "no special characters" condition. Try (?=^(([a-zA-Z0-9]){5,30})$)(?=(\D*\d){1,})(?=([^a-z]*[a-z]){2,})(?=([^A-Z]*[A-Z]){4,}) – TonyR Aug 17 '20 at 06:24
  • Oh yeah I missed that. Updating... Also make sure to put the anchor first on yours. – pguardiario Aug 17 '20 at 06:32
  • https://regex101.com/r/w9BMgW/6 https://regex101.com/r/w9BMgW/7 Unfortunately, it seems like neither worked – Bob Johnson Aug 17 '20 at 14:49
  • And if this helps, I found this: \b(?=(?:[a-z\d]*[A-Z]){3})(?=.*\d)(?=(?:[A-Z\d]*[a-z]){2})[a-zA-Z\d]{5,30}\b does everything I want except not to match 123 , I had tried asserting a lookahead that says at least 1 digit is a prerequisite, that is(?=.*\d) but it still detects 123, any idea on how to fix it? – Bob Johnson Aug 17 '20 at 14:59
  • There's something wrong with your assumptions here. – pguardiario Aug 18 '20 at 00:23