MVCNewbzter
MVCNewbzter

Reputation:

regex for alphanumeric word, must be 6 characters long

What is the regex for a alpha numeric word, at least 6 characters long (but at most 50).

Upvotes: 3

Views: 17575

Answers (3)

chroder
chroder

Reputation: 4463

/[a-zA-Z0-9]{6,50}/

You can use word boundaries at the beginning/end (\b) if you want to actually match a word within text.

/\b[a-zA-Z0-9]{6,50}\b/

Upvotes: 15

Peter Boughton
Peter Boughton

Reputation: 112170

\b\w{6,50}\b

\w is any 'word' character - depending on regex flavour it might be just [a-z0-9_] or it might include others (e.g. accented chars/etc).

{6,50} means between 6 and 50 (inclusive)

\b means word boundary (ensuring the word does not exceed the 50 at either end).


After re-reading, it appears that what you want do is ensure the entire text matches? If so...

^\w{6,50}$

Upvotes: 8

Paige Ruten
Paige Ruten

Reputation: 176675

With PCRE regex you could do this:

/[a-zA-Z0-9]{6,50}/

It would be very hard to do in regex without the min/max quantifiers so hopefully your language supports them.

Upvotes: 0

Related Questions