Open In App

How to Make Java Regular Expression Case Insensitive in Java?

Last Updated : 09 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to make Java Regular Expression case-insensitive in Java. Java Regular Expression is used to find, match, and extract data from character sequences. Java Regular Expressions are case-sensitive by default. But with the help of Regular Expression, we can make the Java Regular Expression case-insensitive. There are two ways to make Regular Expression case-insensitive:

  1. Using CASE_INSENSITIVE flag
  2. Using modifier

1. Using CASE_INSENSITIVE flag: The compile method of the Pattern class takes the CASE_INSENSITIVE flag along with the pattern to make the Expression case-insensitive. Below is the implementation:

Java




// Java program demonstrate How to make Java
// Regular Expression case insensitive in Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
class GFG {
    public static void main(String[] args)
    {
  
        // String
        String str = "From GFG class. Welcome to gfg.";
  
        // Create pattern to match along
        // with the flag CASE_INSENSITIVE
        Pattern patrn = Pattern.compile(
            "gfg", Pattern.CASE_INSENSITIVE);
  
        // All metched patrn from str case
        // insensitive or case sensitive
        Matcher match = patrn.matcher(str);
  
        while (match.find()) {
            // Print matched Patterns
            System.out.println(match.group());
        }
    }
}


Output

GFG
gfg

2. Using modifier: The ” ?i “ modifier used to make a Java Regular Expression case-insensitive. So to make the Java Regular Expression case-insensitive we pass the pattern along with the ” ?i ” modifier that makes it case-insensitive. Below is the implementation:

Java




// Java program demonstrate How to make Java
// Regular Expression case insensitive in Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
class GFG {
    public static void main(String[] args)
    {
  
        // String
        String str = "From GFG class. Welcome to gfg.";
  
        // Create pattern to match along
        // with the ?i modifier
        Pattern patrn = Pattern.compile("(?i)gfg");
  
        // All metched patrn from str case
        // insensitive or case sensitive
        Matcher match = patrn.matcher(str);
  
        while (match.find()) {
            // Print matched Patterns
            System.out.println(match.group());
        }
    }
}


Output

GFG
gfg


Similar Reads

How to validate identifier using Regular Expression in Java
Given a string str, the task is to check whether the string is a valid identifier or not using the Regular Expression. The valid rules for defining Java identifiers are: It must start with either lower case alphabet[a-z] or upper case alphabet[A-Z] or underscore(_) or a dollar sign($).It should be a single word, the white spaces are not allowed.It
2 min read
Finding Data Type of User Input using Regular Expression in Java
Given a string, the task is to find its corresponding datatype using regular expression in java. We can broadly classify all data types into following types: Integer: Numeric datatypes like byte, short, int, long take the form of an Integer object.Double: Decimal datatypes like float and double take the form of Double object.Date: Date in any forma
2 min read
Java Program to Find Duplicate Words in a Regular Expression
Given an Expression which is represented by String. The task is to find duplicate elements in a Regular Expression in Java. Use a map or set data structures for identifying the uniqueness of words in a sentence. Examples: Input : str = " Hi, I am Hritik and I am a programmer. " Output: I am Explanation: We are printing duplicate words in the given
3 min read
Java Program to Convert String to String Array Using Regular Expression
Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided under java. util.regex package. This consists of 3 c
3 min read
How to Use Regular Expression as a Substitute of endsWith() Method in Java?
So primarily discuss what is endsWith() method is, so it is a method of String class that checks whether the string ends with a specified suffix. This method returns a boolean value true or false. Syntax: public boolean endsWith(String suff) Parameter: specified suffix part Return: Boolean value, here in java we only have true and false. Methods: W
3 min read
Convert camel case string to snake case in Java
Given a string in camel case, the task is to write a Java program to convert the given string from camel case to snake case and print the modified string. Examples: Input: GeeksForGeeks Output: geeks_for_geeks Input: CamelCaseToSnakeCase Output: camel_case_to_snake_case Method 1: Naive Approach First we initialize a variable 'result' with an empty
3 min read
Java Program to Convert Infix Expression to Postfix expression
In this article let us discuss how to convert an infix expression to a postfix expression using Java. Pre-Requisites:1. Infix expression: Infix expressions are expressions where an operator is in between two operators. It is the generic way to represent an expression or relationship between the operators and operands mathematically. Example: 3 * 5,
5 min read
How to validate MAC address using Regular Expression
Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression. A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits.One way to represent them is to form six pairs of the characters separated with a hyphen (-) or colon(:). For example, 01-23-45-67
6 min read
How to validate Indian driving license number using Regular Expression
Given string str, the task is to check whether the given string is a valid Indian driving license number or not by using Regular Expression.The valid Indian driving license number must satisfy the following conditions: It should be 16 characters long (including space or hyphen (-)).The driving license number can be entered in any of the following f
7 min read
How to validate CVV number using Regular Expression
Given string str, the task is to check whether it is a valid CVV (Card Verification Value) number or not by using Regular Expression. The valid CVV (Card Verification Value) number must satisfy the following conditions: It should have 3 or 4 digits.It should have a digit between 0-9.It should not have any alphabet or special characters. Examples: I
5 min read
Practice Tags :