一个字符串里可以有大写字母,小写字母,数字,标点符号怎么样判断这个字符串只包含两种类型的内容呢?比如
aaa1
aaBB
aaa....a
..1!
谢谢先
aaa1
aaBB
aaa....a
..1!
谢谢先
解决方案 »
- 关于反射机制 获取类的方法参数类型返回值是Class类型的数组的 好处在哪里
- s.replaceAll("^([0-9]^) +", "^1,")
- 请问谁有学习多线程的例子哦?
- PHP 中的ECHO JAVASCRIPT 乱码
- 看看这个问题:
- [求助]小弟剩下的分都填上了,关于解析HTML文本的问题
- JAVA里怎样处理oracle里的long类型的字段
- [求助]请问如何把JTextField的值设置为空???谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!
- replace的使用
- 高手到哪去了??????????????????????????????
- 请教一个如何在hashmap中查找以特定字符串开头的键值!
- java jni [javah] 包名的问题............
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)