java 大小写匹配_大写字母的Java正则表达式
我開(kāi)始熟悉Java,但是我仍然發(fā)現(xiàn)正則表達(dá)式很混亂。我需要檢查一個(gè)元素是否有連續(xù)的兩個(gè)大寫(xiě)字母,連續(xù)的三個(gè)大寫(xiě)字母,連續(xù)的四個(gè)大寫(xiě)字母,或者一個(gè)大寫(xiě)字母,一個(gè)空格,然后是另一個(gè)大寫(xiě)字母。這是迄今為止的代碼,但我覺(jué)得有一個(gè)更好的方法來(lái)做到這一點(diǎn)。
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 = Pattern.compile("[A-Z][A-Z][A-Z][A-Z]");
Pattern q = Pattern.compile("[A-Z] [A-Z]");
Pattern r = Pattern.compile("[A-Z][A-Z]");
Pattern s = Pattern.compile("[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());
}
}
}任何幫助,將不勝感激。
總結(jié)
以上是生活随笔為你收集整理的java 大小写匹配_大写字母的Java正则表达式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Day24-Ajax
- 下一篇: Sublime text代码补全插件(支