「Java」PatternとMatcherで半角英小文字を判断する

2021年9月30日

構文
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 英小文字に含まれる

Java

Posted by arkgame