views:

2899

answers:

3

Hi,

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

A: 

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.

yjerem
This will match 50 characters in a word of 51 or more characters, which is not what the person asking the question wants.
Jan Goyvaerts
I assumed he knew some regex, I was just showing him how to use min/max quantifiers. The slashes are just there to show it's a regex...
yjerem
+11  A: 
/[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/
Christopher Nadeau
i want to match the entire text, not a subset of it. so I need to word boundaries then right?
MVCNewbzter: Yes, that is correct
HanClinto
Actually, no, if you want to match the entire string rather than just looking to see if it contains a matching substring, you need to anchor it with ^ and $ like so: /^[a-zA-Z0-9]{6,50}$/
Dave Sherohman
+7  A: 
\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}$
Peter Boughton