Java Programing laungage

java.util Projects

java.util Project 1

Setting Case Sensitivity in Regular Expression

In This programming tutorials we are try to describes the method to set the pattern is either case sensitive or not. Actually, by default the pattern is case sensitive. Whenever it need to be set for the case insensitivity then we have to use the appropriate code program.

Previous Home Next
Code Description

Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE)

This code is used for setting the pattern in case sensitive because it is by default case sensitive mode. By passing the Pattern.CASE_INSESITIVE field of Pattern class is passed with the string, which is compiled for searching as a parameter of the compile() method of the class.

Example


package r4r;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class setcasesencitivitytest {
	
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the string: ");
String string = br.readLine();
System.out.print("Enter string for search: ");
String str = br.readLine();
Pattern pat = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
Matcher match = pat.matcher(string);
String afterReplace = match.replaceAll("S");
System.out.println(afterReplace);
}
}

Previous Home Next