「java」正規表現で小文字の半角英字をチェックする方法

説明
public static boolean matches(String regex, CharSequence input)
指定された正規表現をコンパイルして、指定された入力とその正規表現をマッチします。
このメソッドを次の形式で呼び出すと、上記の動作が行われます。
パラメータ:
regex – コンパイルされる表現
input – マッチされる文字シーケンス
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame;
import java.util.regex.Pattern;
public class HalfUpperDemo {
public final static String chkPattern = "^[a-z]+$";
public static void main(String[] args) {
String strA = "tokyo";
String strB = "Yokohama";
String strC = "fukuOka";
boolean flgA = isHalfUpperAlpha(strA);
boolean flgB = isHalfUpperAlpha(strB);
boolean flgC = isHalfUpperAlpha(strC);
if (flgA == true) {
System.out.println(strA + " is " + "小文字の半角英字");
} else {
System.out.println(strA + " is not " + "小文字の半角英字");
}
if (flgB == true) {
System.out.println(strB + " is " + "小文字の半角英字");
} else {
System.out.println(strB + " is not " + "小文字の半角英字");
}
if (flgC == true) {
System.out.println(strC + " is " + "小文字の半角英字");
} else {
System.out.println(strC + " is not " + "小文字の半角英字");
}
}
public static boolean isHalfUpperAlpha(String target) {
return Pattern.matches(chkPattern, target);
}
}
package com.arkgame; import java.util.regex.Pattern; public class HalfUpperDemo { public final static String chkPattern = "^[a-z]+$"; public static void main(String[] args) { String strA = "tokyo"; String strB = "Yokohama"; String strC = "fukuOka"; boolean flgA = isHalfUpperAlpha(strA); boolean flgB = isHalfUpperAlpha(strB); boolean flgC = isHalfUpperAlpha(strC); if (flgA == true) { System.out.println(strA + " is " + "小文字の半角英字"); } else { System.out.println(strA + " is not " + "小文字の半角英字"); } if (flgB == true) { System.out.println(strB + " is " + "小文字の半角英字"); } else { System.out.println(strB + " is not " + "小文字の半角英字"); } if (flgC == true) { System.out.println(strC + " is " + "小文字の半角英字"); } else { System.out.println(strC + " is not " + "小文字の半角英字"); } } public static boolean isHalfUpperAlpha(String target) { return Pattern.matches(chkPattern, target); } }
package com.arkgame;

import java.util.regex.Pattern;

public class HalfUpperDemo {

      public final static String chkPattern = "^[a-z]+$";

      public static void main(String[] args) {

            String strA = "tokyo";
            String strB = "Yokohama";
            String strC = "fukuOka";
            boolean flgA = isHalfUpperAlpha(strA);
            boolean flgB = isHalfUpperAlpha(strB);
            boolean flgC = isHalfUpperAlpha(strC);

            if (flgA == true) {
                  System.out.println(strA + " is " + "小文字の半角英字");
            } else {
                  System.out.println(strA + " is not " + "小文字の半角英字");
            }
            if (flgB == true) {
                  System.out.println(strB + " is " + "小文字の半角英字");
            } else {
                  System.out.println(strB + " is not " + "小文字の半角英字");
            }
            if (flgC == true) {
                  System.out.println(strC + " is " + "小文字の半角英字");
            } else {
                  System.out.println(strC + " is not " + "小文字の半角英字");
            }

      }

      public static boolean isHalfUpperAlpha(String target) {
            return Pattern.matches(chkPattern, target);
      }

}

結果 
tokyo is 小文字の半角英字
Yokohama is not 小文字の半角英字
fukuOka is not 小文字の半角英字

Java

Posted by arkgame