Home > Net >  Matching alphanumeric words in a string which are greater than 2 characters in length
Matching alphanumeric words in a string which are greater than 2 characters in length

Time:11-11

I have following regex.

/^(.*[^0-9])(.[a-z] [0-9] [a-z0-9]*|[0-9] [a-z] [a-z0-9]*{3,})(.*)$/gm

I basically want to match alphanumeric groups in an URL which are greater than 2 characters in length. So basically:

In the URL: /version/a1/type/eg1234/abc, eg1234 should match since its alphanumeric and greater than 2 in length.

However, while my alphanumeric match logic seems to be working fine, the length condition i.e. {3,} isn't being satisfied, as in e.g. /version/a1/type/, the regex also matches a1 which it shouldn't as its less than 2 characters in length.

Please help me in correcting my regex.

CodePudding user response:

Try:

(?=\d [a-z][a-z\d]*|[a-z\d]*[a-z]\d)[a-z\d]{3,}

Regex demo.

This will match only eg1234 in /version/a1/type/eg1234/abc

CodePudding user response:

Try:

/\b(?=[a-zA-Z0-9]{4,})(?=(?:[^\/\d]*\d){4,})([^\/]{3,})/

Demo

  • Related