Java Programing laungage

java.util Projects

java.util Project 1

Compiling a Pattern with Multiple Flags

In this programming tutorials, we are going to learn how to compile a pattern with multiple flags like multiline, case insensitivity of the characters in the string etc. This page of tutorials provides us an example for the implementation of the concept about how we can do this type of task. this example has been used some APIs for compiling pattern with multiple flags.

Previous Home Next
Code Description

Pattern.compile(patternStr, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE)

This method is used to compile the string assigned with the String type variable patStr with multiple flags which are given follows.

  1. patStr

  2. This is the string which has to be compiled.

  3. Pattern.MULTILINE

  4. This is the field of the Pattern class which denotes for the multiline characters in the given string. This field determines the compilation of the string with the multiline character which breaks the string after a specific part.

  5. Pattern.CASE_INSENSITIVE

  6. This is also a field of the Pattern class which is used to denote the case sensitivity of the character in the given String. By default pattern is compile the string in case sensitive mode. If we pass the CASE_INSENSITIVE field through the compile() method as parameter, it is ignore the case of characters in the string.

Example


package r4r;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class multiflagtest {
public static void main(String[] args){
String putStr = "Abc\ndef";//Print the original string.
System.out.println(putStr);
String patStr = "\n";
Pattern pat = Pattern.compile(patStr, Pattern.MULTILINE |  
// Compile with multiline and case-insensitive .

		Pattern.CASE_INSENSITIVE);
Matcher match = pat.matcher(putStr);
boolean matchFound = match.find(); 
//Finding new line and replacing with a tab.
String str = match.replaceAll("\t");
System.out.println(matchFound);
System.out.println(str);
String[] str1 = "Abc\ndef".split("\n");
//Split from where the new line character lies in the string.
for (int i =1; i<str1.length; i++){
System.out.print(str1[i] + " ");
}
}
}

Previous Home Next