「Java」matches()で文字列と正規表現が一致するかどうかを判定する

説明
public boolean matches(String regex)
この文字列が、指定された正規表現と一致するかどうかを判定します。
パラメータ:
regex – この文字列との一致を判定する正規表現
戻り値:
この文字列が指定された正規表現と一致する場合にだけ、trueが返される
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class MatchDemo {
protected static final String regexA = "[a-z0-9]+";
protected static final String regexB = "[a-z]+";
public static void main(String[] args) {
String strA = "test5679";
if (strA.matches(regexA)) {
System.out.println("正規表現Aと完全一致");
} else {
System.out.println("正規表現Aと一致しない");
}
if (strA.matches(regexB)) {
System.out.println("正規表現Bと完全一致");
} else {
System.out.println("正規表現Bと一致しない");
}
}
}
package com.arkgame.study; public class MatchDemo { protected static final String regexA = "[a-z0-9]+"; protected static final String regexB = "[a-z]+"; public static void main(String[] args) { String strA = "test5679"; if (strA.matches(regexA)) { System.out.println("正規表現Aと完全一致"); } else { System.out.println("正規表現Aと一致しない"); } if (strA.matches(regexB)) { System.out.println("正規表現Bと完全一致"); } else { System.out.println("正規表現Bと一致しない"); } } }
package com.arkgame.study;

public class MatchDemo {

      protected static final String regexA = "[a-z0-9]+";
      protected static final String regexB = "[a-z]+";

      public static void main(String[] args) {
            String strA = "test5679";

            if (strA.matches(regexA)) {
                  System.out.println("正規表現Aと完全一致");
            } else {
                  System.out.println("正規表現Aと一致しない");
            }
            if (strA.matches(regexB)) {
                  System.out.println("正規表現Bと完全一致");
            } else {
                  System.out.println("正規表現Bと一致しない");
            }

      }

}

実行結果
正規表現Aと完全一致
正規表現Bと一致しない

Java

Posted by arkgame