Questions asking for code must demonstrate a minimal understanding of the problem being solved. Inclu开发者_运维问答de attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this questionTo validate my string, I need the string only contains lowercase or uppercase or both cases mixed characters from A(a) to Z(z), and string length should be in the range from 6 to 12 characters long. What's the regular expression for this restrain?
You could use this regular expression:
^[\p{Lu}\p{Ll}\p{Lt}]{6,12}$
if you didn't want to penalize people for being named François, María, or Fuß.
Of course, what a string's length is in Java is less than perfectly clear, especially here, since the Pattern
and Matcher
classes only deal with lengths in code points (logical Unicode characters), not in the length of the string in Java's built-in but very oxymoronically named char
units (physical 16-bit pieces of UTF-16).
That means that strings with surrogates will appears to have a different length from the standpoint of the regex engine than from many other Java classes.
The regex engine has it correct, BTW.
This should work:
[A-Za-z]{6,12}
However, it will match an instance of your criteria inside of the input string, so you can add the ^
and $
anchors to ensure your entire input string conforms to your criteria.
^[A-Za-z]{6,12}$
Try this:
[a-zA-Z]{6,12}
精彩评论