我试图找到字符串中所有基于案例的字符是否跟在非基础字符之后。即非基于案例的字符为大写字母,所有其他基于案例的字符均为小写
示例:您的测试是什么
我定义的正则表达式是:\b[A-Z]
String alphaCheck="What Is Your Test";
System.out.println(alphaCheck.matches("\b[A-Z]"));
答案 0 :(得分:1)
([A-Z][a-z]+(\\s+|$))+
适合您:
public static void main(String[] args) {
String s = "What Is Your Test";
System.out.println(s.matches("([A-Z][a-z]+(\\s+|$))+"));
String s2 = "What is";
System.out.println(s2.matches("([A-Z][a-z]+(\\s+|$))+"));
}
O / P:
true
false
答案 1 :(得分:0)
作为另一种选择,您可以使用否定方式,并检查是否有任何不符合您的标准:
String alphaCheck="What Is Your Test";
System.out.println(!alphaCheck.matches(".*\\b[a-z]+.*"));
alphaCheck="What Is Your test";
System.out.println(!alphaCheck.matches(".*\\b[a-z]+.*"));
alphaCheck="what Is Your Test";
System.out.println(!alphaCheck.matches(".*\\b[a-z]+.*"));
O / P:
true
false
false
您只需要还原String#matches
返回以表示是否正确。