1

How can I write a regex to check that a string is alpha-numeric and has a maximum of 11 characters?

2 Answers 2

4
/^\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}$/
Sign up to request clarification or add additional context in comments.

8 Comments

@ithcy: POSIX character classes are rather passé in that they tend to work only with the nasty, old-style 8-bit locales. They don’t make much sense in a Unicode world, at least not without extensions. It depends on whether they’re compliant with UTS#18.
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.
@tchrist: Which characters except for alphanumeric and underscore match \w?
@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.
@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 '_'"
|
1

Um, no.

 /\A[\p{Alphabetic}\pN]{1,11}\z/

is the right answer to the question asked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.