How can I write a regex to check that a string is alpha-numeric and has a maximum of 11 characters?
2 Answers
/^\w{1,11}$/
\w means any alphanumeric character or underscore in a perl regex. If you don't want underscores:
/^[a-zA-Z0-9]{1,11}$/
8 Comments
tchrist
Actually,
\w in a Perl regex does not mean an alphanumunder, contrary to popular misunderstanding. Rather, it means exactly [\p{Alphabetic}\p{Decimal_Number}\p{Mark}\p{Connector_Punctuation}]. Close, but not equivalent.Tim
@tchrist: Which characters except for alphanumeric and underscore match
\w?tchrist
@Tim: I just told you: Marks, Connector Punctuation. Also, Alphabetic includes Other_Alphabetic; it is certainly not just Letters. Also,
\w is only Decimal Numbers and Letter Numbers; there are other numeric types it does not match. You should get unichars to play around with this. You’ll likely also want uniprops and perhaps uninames, too.Tim
@tchrist: You are right. Looks like perlre made the same mistake: "A '\w' matches a single alphanumeric character (an alphabetic character, or a decimal digit) or '_'"
|