你用这个试一下 Pattern p = Pattern.compile("^([a-zA-Z_0-9]||[\\p{Punct}])*$"); Matcher m = p.matcher("ffq wer ty uo."); System.out.println(m.matches());
上面的没加空格判断有点问题 Pattern p = Pattern.compile("^([a-zA-Z_0-9]||[\\p{Punct}]||[\\p{Blank}])*$"); Matcher m = p.matcher("地区性This is me!"); System.out.println(m.matches());上面的代码把中文去掉就可以返加true了.
\p{P} 表示所有的 Unicode 标点符号,包括全角、半角的,横排、直排的标点符号。bitCount 是计算一个 int 类型数据中二进制位 1 的数量,比如 13(1101)结果为 3,即“1”的个数有 3 个。算法是 Sun 引用 Hacker's Delight(中文书名《高效程序的奥秘》),采用位运算得出。
Lu Letter, Uppercase Ll Letter, Lowercase Lt Letter, Titlecase Lm Letter, Modifier Lo Letter, Other Mn Mark, Nonspacing Mc Mark, Spacing Combining Me Mark, Enclosing Nd Number, Decimal Digit Nl Number, Letter No Number, Other Pc Punctuation, Connector Pd Punctuation, Dash Ps Punctuation, Open Pe Punctuation, Close Pi Punctuation, Initial quote (may behave like Ps or Pe depending on usage) Pf Punctuation, Final quote (may behave like Ps or Pe depending on usage) Po Punctuation, Other Sm Symbol, Math Sc Symbol, Currency Sk Symbol, Modifier So Symbol, Other Zs Separator, Space Zl Separator, Line Zp Separator, Paragraph Cc Other, Control Cf Other, Format Cs Other, Surrogate Co Other, Private Use Cn Other, Not Assigned (no characters in the file have this property)
char[] chars = str.toCharArray();
int lowercase = 1;
int uppercase = 2;
int number = 4;
int punctuation = 8;
int type = 0;
Pattern pattern = Pattern.compile("\\p{P}");
for (int i = 0; i < chars.length; i++) {
if (chars[i] >= '0' && chars[i] <= '9') {
type |= number;
continue;
}
if (chars[i] >= 'a' && chars[i] <= 'z') {
type |= lowercase;
continue;
}
if (chars[i] >= 'A' && chars[i] <= 'Z') {
type |= uppercase;
continue;
}
if (pattern.matcher(chars[i] + "").matches()) {
type |= punctuation;
continue;
}
}
return Integer.bitCount(type) == 2;
}
对啊,包含任意两种类型的话返回 true,否则返回 false
\w 单词字符:[a-zA-Z_0-9] 就可以
但标点符号就麻烦一点,
要加
\p{Punct} 标点符号:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Pattern p = Pattern.compile("^([a-zA-Z_0-9]||[\\p{Punct}])*$");
Matcher m = p.matcher("ffq wer ty uo.");
System.out.println(m.matches());
Pattern p = Pattern.compile("^([a-zA-Z_0-9]||[\\p{Punct}]||[\\p{Blank}])*$");
Matcher m = p.matcher("地区性This is me!");
System.out.println(m.matches());上面的代码把中文去掉就可以返加true了.
\p{P} 表示所有的 Unicode 标点符号,包括全角、半角的,横排、直排的标点符号。
你怎么知道的啊?奇怪...
Lu Letter, Uppercase
Ll Letter, Lowercase
Lt Letter, Titlecase
Lm Letter, Modifier
Lo Letter, Other
Mn Mark, Nonspacing
Mc Mark, Spacing Combining
Me Mark, Enclosing
Nd Number, Decimal Digit
Nl Number, Letter
No Number, Other
Pc Punctuation, Connector
Pd Punctuation, Dash
Ps Punctuation, Open
Pe Punctuation, Close
Pi Punctuation, Initial quote (may behave like Ps or Pe depending on usage)
Pf Punctuation, Final quote (may behave like Ps or Pe depending on usage)
Po Punctuation, Other
Sm Symbol, Math
Sc Symbol, Currency
Sk Symbol, Modifier
So Symbol, Other
Zs Separator, Space
Zl Separator, Line
Zp Separator, Paragraph
Cc Other, Control
Cf Other, Format
Cs Other, Surrogate
Co Other, Private Use
Cn Other, Not Assigned (no characters in the file have this property)