English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Поле CASE_INSENSITIVE в классе Pattern Java и примеры

The CASE_INSENSITIVE field of the Pattern class matches characters regardless of case. When this value is used ascompile()method flag value, and if using regular expression search characters, characters in both cases will match.

Note-By default, this flag only matches ASCII characters

Example 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+"]";
      //Regular expression compilation
      //compile regular expression
      //search 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)");
   }
}

output result

Enter input data:
oldtoolbag.com 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
Буква T встречается 20 раз в данном тексте (независимо от регистра)

пример 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("Введите строковое значение: ");
      String str = sc.next();
      Pattern pattern = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(str);
      if(matcher.matches()){
         System.out.println("Даная строка является типом boolean");
      } else {
         System.out.println("Даная строка не является типом boolean");
      }
   }
}

Вывод 1

Введите строковое значение:
истина
Даная строка является типом boolean

Вывод 2

Введите строковое значение:
ложно
Даная строка является типом boolean

Вывод 3

Введите строковое значение:
hello
Даная строка не является типом boolean
Основной учебник
Рекомендуем к просмотру