1

I want to make a single regular expression that is 10-20 in length, has to have a combination of at least 1 string and 1 number, cannot contain anything else and the first character has to be a string. I have tried

(?=.*[A-Za-z])(?=.*\d)[A-Za-z0-9]{10,20}

but it matches with things like <73010825822784709>d which isn't optimal, any help?

Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
  • Use anchors, `^(?=.*[A-Za-z])(?=.*\d)[A-Za-z0-9]{10,20}$` – Wiktor Stribiżew Aug 15 '20 at 17:56
  • Yeah, but the objective me writing this regex is so I can detect strings that are in the end of an url, for example https://youtube.com/abcDeFg1234, I want it to detect the last sequence which I can't do with anchors. – ForeverClueless Aug 15 '20 at 18:00
  • Great, then `/\/(?=\d*[A-Za-z])(?=[a-zA-Z]*\d)[A-Za-z0-9]{10,20}$/` will do (if the URL is at the end of string). If not, you need to specify what the end of the URL is signalled with. – Wiktor Stribiżew Aug 15 '20 at 18:07
  • Opps I guess I wasn't being specific, I'm making this regex for a discord server, which contains messages, please make it so it detects sequences like "hey guys check out https://youtube.com/abcDeFg1234 its awesome!" then that'll work perfectly – ForeverClueless Aug 15 '20 at 18:18
  • Then it will look like `/\/(?=\d*[A-Za-z])(?=[a-zA-Z]*\d)[A-Za-z0-9]{10,20}(?!\S)/` – Wiktor Stribiżew Aug 15 '20 at 18:28
  • Sorry for being annoying but your regex works fine, except 1 thing, if they put symbols like | after the URL, such as https://youtube.com/abcDeFg1234|, it wont detect. – ForeverClueless Aug 15 '20 at 18:59

1 Answers1

2

Use

/\/(?=\d*[A-Za-z])(?=[a-zA-Z]*\d)[A-Za-z0-9]{10,20}\b/

See proof

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \d*                      digits (0-9) (0 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    [A-Za-z]                 any character of: 'A' to 'Z', 'a' to 'z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [a-zA-Z]*                any character of: 'a' to 'z', 'A' to 'Z'
                             (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [A-Za-z0-9]{10,20}       any character of: 'A' to 'Z', 'a' to 'z',
                           '0' to '9' (between 10 and 20 times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37