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

Campo CASE_INSENSITIVE del patrón en Java y ejemplo

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

Note-By default, this flag only matches ASCII characters

ejemplo1

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+"]"
      //Compile regular expression
      Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
      //Search matcher object
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      while (matcher.find()) {
         ++;
      {
      System.out.println("The letter "+ch+"occurred"++"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.
Ingrese el carácter requerido:
T
La letra T ocurrió 20 veces en el texto dado (independientemente del caso)

ejemplo2

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("Ingrese un valor de cadena: ");
      String str = sc.next();
      Pattern pattern = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(str);
      if(matcher.matches()){
         System.out.println("La cadena dada es de tipo booleano");
      } else {
         System.out.println("La cadena dada no es de tipo booleano");
      {
   {
{

Salida1

Ingrese un valor de cadena:
verdadero
La cadena dada es de tipo booleano

Salida2

Ingrese un valor de cadena:
falso
La cadena dada es de tipo booleano

Salida3

Ingrese un valor de cadena:
hola
La cadena dada no es de tipo booleano