例を使用したJavaのパターンCASE_INSENSITIVEフィールド
PatternクラスのこのCASE_INSENSITIVEフィールドは、大文字と小文字に関係なく文字と一致します。これをcompile()メソッドのフラグ値として使用し、正規表現を使用して文字を検索すると、両方の場合の文字が一致します。
注 −デフォルトでは、このフラグはASCII文字のみに一致します
例1
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];
//Regular expression to find the required character
String regex = "["+ch+"]";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
//Retrieving the matcher object
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)
例2
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
-
例を使用したJavaのパターンマッチャー()メソッド
java.util.regex javaのパッケージは、文字シーケンスの特定のパターンを見つけるためのさまざまなクラスを提供します。 このパッケージのパターンクラスは、正規表現のコンパイル済み表現です。 matcher() このクラスのメソッドは、 CharSequenceのオブジェクトを受け入れます 入力文字列を表すクラスとは、指定された文字列を現在の(パターン)オブジェクトで表される正規表現に一致させるMatcherオブジェクトを返します。 例 import java.util.Scanner; import java.util.regex.Matcher; import java
-
例を使用したJavaのパターンcompile()メソッド
java.regexのパターンクラス packageは、正規表現をコンパイルしたものです。 compile() このクラスのメソッドは、正規表現を表す文字列値を受け入れ、Patternオブジェクトを返します。 例 import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CompileExample { public static void main( String args[] ) { &