我开始熟悉Java,但是我仍然发现正则表达式很混乱。我需要检查一个元素是否有连续的两个大写字母,连续的三个大写字母,连续的四个大写字母,或者一个大写字母,一个空格,然后是另一个大写字母。这是迄今为止的代码,但我觉得有一个更好的方法来做到这一点。
public class First {
public static void main(String[] args) {
String one = "A scenario - CAPI 200 - 001";
String two = "A scenario - C T 200 - 001";
String three = "A scenaRio - CT 200 - 001";
String four = "A sCenario - CAP 200 - 001";
Pattern p = pile("[A-Z][A-Z][A-Z][A-Z]");
Pattern q = pile("[A-Z] [A-Z]");
Pattern r = pile("[A-Z][A-Z]");
Pattern s = pile("[A-Z][A-Z][A-Z]");
Matcher m =p.matcher(one);
if (m.find()){
System.out.println(m.group());
}
Matcher d =q.matcher(two);
if (d.find()){
System.out.println(d.group());
}
Matcher e =r.matcher(three);
if (e.find()){
System.out.println(e.group());
}
Matcher a =s.matcher(four);
if (a.find()){
System.out.println(a.group());
}
}
}任何帮助,将不胜感激。