「Java」PatternとMatcherで半角英大文字を判断する
構文
1.public static Pattern compile(String regex)
指定された正規表現をパターンにコンパイルします。
2.public Matcher matcher(CharSequence input)
指定された入力とこのパターンをマッチする正規表現エンジンを作成します。
使用例
package com.arkgame.bat;
import java.util.regex.Pattern;
public class EsampDemo {
/** 英大文字の正規表現 */
private static final Pattern ALP_UPPER = Pattern.compile("[A-Z]+");
public static void main(String[] args) {
// 小文字
String strA = "study skill";
// 大文字
String strB = "ARKGAME";
// 小文字大文字混在
String strC = "Com";
// 関数を呼び出す
checkfunc(strA);
checkfunc(strB);
checkfunc(strC);
}
public static void checkfunc(String target) {
// 指定された入力とこのパターンをマッチする正規表現エンジンを作成します
if (ALP_UPPER.matcher(target).find()) {
System.out.println(target + " 英大文字に含まれる");
} else {
System.out.println(target + " 英大文字に含まれない");
}
}
}
実行結果
study skill 英大文字に含まれない
ARKGAME 英大文字に含まれる
Com 英大文字に含まれる