Java
 Computer >> コンピューター >  >> プログラミング >> Java

JavaのPattern.CASE_INSENSITIVEフラグとは?使い方を実行例付きで解説

JavaのPatternクラスが持つCASE_INSENSITIVEフィールドは、大文字と小文字を区別せずに文字をマッチングさせるための正規表現フラグです。このフラグをcompile()メソッドに渡してパターンをコンパイルすると、正規表現による検索時に大文字・小文字のどちらの文字でもマッチするようになります。

注意:デフォルトでは、このフラグはASCII文字(半角英数字)のみを対象としてマッチングを行います。Unicode文字全体を対象にしたい場合は、UNICODE_CASEフラグと組み合わせて使用します。

例1:指定した文字の出現回数を大文字小文字を区別せずにカウントする

次のプログラムでは、ユーザーから入力されたテキストと1文字を受け取り、CASE_INSENSITIVEフラグを使って、その文字がテキスト中に何回出現するかを大文字・小文字を問わずにカウントしています。

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CASE_INSENSITIVE_Example {
    public static void main( String args[] ) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter input data: ");
        String input = sc.nextLine();
        System.out.println("Enter required character: ");
        char ch = sc.next().toCharArray()[0];
        // 目的の文字を見つけるための正規表現
        String regex = "["+ch+"]";
        // 正規表現をコンパイル(CASE_INSENSITIVEフラグを指定)
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        // Matcherオブジェクトを取得
        Matcher matcher = pattern.matcher(input);
        int count =0;
        while (matcher.find()) {
            count++;
        }
        System.out.println("The letter "+ch+" occurred "+count+" times in the given text (irrespective of case)");
    }
}

実行結果

Enter input data:
Tutorials Point originated from the idea that there exists a class 
of readers who respond better to online content and prefer to learn 
new skills at their own pace from the comforts of their drawing rooms.
Enter required character:
T
The letter T occurred 20 times in the given text (irrespective of case)

この実行結果からわかるように、「T」と検索した場合でも「t」も含めてマッチしているため、合計20回というカウント結果になっています。

例2:入力された文字列がboolean値かどうかを判定する

次の例では、CASE_INSENSITIVEフラグを使用して、入力された文字列が「true」または「false」(大文字・小文字は問わない)であるかどうかを判定します。

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VerifyBoolean {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string value: ");
        String str = sc.next();
        Pattern pattern = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        if(matcher.matches()){
            System.out.println("Given string is a boolean type");
        } else {
            System.out.println("Given string is not a boolean type");
        }
    }
}

実行結果1

Enter a string value:
true
Given string is a boolean type

実行結果2

Enter a string value:
false
Given string is a boolean type

実行結果3

Enter a string value:
hello
Given string is not a boolean type

まとめ

Pattern.CASE_INSENSITIVEは、正規表現で大文字・小文字を区別せずにマッチングを行いたい場合に便利なフラグです。Pattern.compile()メソッドの第2引数として指定するだけで簡単に利用でき、文字列の検証や文字数のカウントなど、さまざまな場面で活用できます。

  1. JavaのPattern.matcher()メソッドの使い方を実例で解説

    Javaのjava.util.regexパッケージには、文字シーケンスの中から特定のパターンを検索するためのさまざまなクラスが用意されています。 このパッケージに含まれるPatternクラスは、正規表現をコンパイルした結果を表すクラスです。このクラスが提供するmatcher()メソッドは、入力文字列を表すCharSequenceオブジェクトを引数として受け取り、現在のPatternオブジェクトが表す正規表現と入力文字列を照合するためのMatcherオブジェクトを返します。 matcher()メソッドの基本的な流れ matcher()メソッドを使った正規表現マッチングは、一般的に以下の手順で行い

  2. 【Java】Pattern.compile()メソッドの使い方を具体例でわかりやすく解説

    java.util.regex パッケージに含まれる Pattern クラスは、正規表現をコンパイルした結果を表すクラスです。このクラスが持つ compile() メソッドは、正規表現を表す文字列を受け取り、その正規表現をコンパイルした Pattern オブジェクトを返します。 基本的な構文 compile() メソッドの最もシンプルな形式は、引数として正規表現の文字列を1つ受け取ります。 Pattern pattern = Pattern.compile(regex); コンパイル済みの Pattern オブジェクトから matcher() メソッドを呼び出すことで Matcher オブジェ