Sunday, 2 November 2014

Pattern.DOTALL

Enables dotall mode. In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.

For Example, address is spawned into multiple lines.

String data = "Address\n" +
"Country: India\n" +
"State: AP\n" +
"City: Guntur\n" +
"Village: Ongole" +
"mailId: 12345676@abc.com";

Now I want to extract upto the field city. Below statements do that.

static Pattern addressPattern = Pattern.compile("(.*)Village:", Pattern.DOTALL);
matcher = addressPattern.matcher(data);

import java.util.regex.*;

public class RegExHarness {
    static Pattern addressPattern = Pattern.compile("(.*)Village:", Pattern.DOTALL);
    static Matcher matcher; 
          
    public static void main(String[] args) {
        String data = "Address\n" +
                    "Country: India\n" +
                    "State: AP\n" +
                    "City: Guntur\n" +
                    "Village: Ongole" +
                    "mailId: 12345676@abc.com";

        RegExHarness obj = new RegExHarness();
 
        System.out.println(data + "\n");

        matcher = addressPattern.matcher(data);
        
        while (matcher.find()) {
            System.out.println(matcher.group(1));
 }
    }
}

Output
Address
Country: India
State: AP
City: Guntur
Village: OngolemailId: 12345676@abc.com

Address
Country: India
State: AP
City: Guntur



Prevoius                                                 Next                                                 Home

Pattern.COMMENTS

Permits whitespace and comments in the pattern. In this mode, whitespace is ignored, and embedded comments starting with # are ignored until the end of a line.

With Pattern.COMMENTS you can put whitespace in your regex like below.

regex = " \\d{9} \\d ";

white spaces are ignored in above case, if you called the compile method the Pattern.COMMENTS option.

import java.util.regex.*;
import java.io.*;

public class RegExHarness {
    public static void main(String args[]) throws IOException{
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        Pattern pattern;
        Matcher matcher;
        
        String regex;
        String text;

        System.out.println("Enter Regular Expression");
        regex = "   \\d{9}  \\d   ";
        pattern = Pattern.compile(regex, Pattern.COMMENTS);   
        
        String[] input = { "1234567890", 
                           "9876543210"
                        };
        
        int i = 0;
        while(i < input.length){
            text = input[i];
                
            matcher = pattern.matcher(text);

            while (matcher.find()) {
                System.out.print("I found the text " + matcher.group());
                System.out.print(" starting at index " + matcher.start());
                System.out.println(" Ending at index " + matcher.end());
            }
            i++;
        }     
    }
}

Output
Enter Regular Expression
I found the text 1234567890 starting at index 0 Ending at index 10
I found the text 9876543210 starting at index 0 Ending at index 10



Prevoius                                                 Next                                                 Home

Pattern.CASE_INSENSITIVE

Perform matching that is not case-sensitive.

import java.util.regex.*;
import java.io.*;

public class RegExHarness {
    public static void main(String args[]) throws IOException{
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        Pattern pattern;
        Matcher matcher;
        
        String regex;
        String text;

        System.out.println("Enter Regular Expression");
        regex = "abc*";
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);   
        
        String[] input = { "abcccc", 
                            "ABCCC", 
                            "ABCccC"
                        };
        
        int i = 0;
        while(i < input.length){
            text = input[i];
                
            matcher = pattern.matcher(text);

            while (matcher.find()) {
                System.out.print("I found the text " + matcher.group());
                System.out.print(" starting at index " + matcher.start());
                System.out.println(" Ending at index " + matcher.end());
            }
            i++;
        }     
    }
}


Output
Enter Regular Expression
I found the text abcccc starting at index 0 Ending at index 6
I found the text ABCCC starting at index 0 Ending at index 5
I found the text ABCccC starting at index 0 Ending at index 6





Prevoius                                                 Next                                                 Home

Saturday, 1 November 2014

Pattern.CANON_EQ

Enables canonical equivalence. Different Unicode encodings of the same character are considered equivalent.


import java.util.regex.*;
import java.io.*;

public class RegExHarness {
    public static void main(String args[]) throws IOException{
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        Pattern pattern;
        Matcher matcher;
        
        String regex;
        String text;

        System.out.println("Enter Regular Expression");
        regex = "\u00e9gal";
        pattern = Pattern.compile(regex, Pattern.CANON_EQ);   
        
        String[] input = { "\u00e9gal", 
                            "e\u0301gal", 
                        };
        
        int i = 0;
        while(i < input.length){
            text = input[i];
                
            matcher = pattern.matcher(text);

            while (matcher.find()) {
                System.out.print("I found the text " + matcher.group());
                System.out.print(" starting at index " + matcher.start());
                System.out.println(" Ending at index " + matcher.end());
            }
            i++;
        }     
    }
}

Output
Enter Regular Expression
I found the text égal starting at index 0 Ending at index 4
I found the text égal starting at index 0 Ending at index 5



Prevoius                                                 Next                                                 Home

Creating a Pattern with Flags

Pattern class defines set of constants that you can use with 'compile' method that accepts a set of flags affecting the way the pattern is matched.

Constants specified in Pattern class
Pattern.CANON_EQ
Pattern.CASE_INSENSITIVE
Pattern.COMMENTS
Pattern.DOTALL
Pattern.LITERAL
Pattern.MULTILINE
Pattern.UNICODE_CASE
Pattern.UNICODE_CHARACTER_CLASS
Pattern.UNIX_LINES




Prevoius                                                 Next                                                 Home

Compiling regular expression

If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than using the 'matches' method every time. Pattern class provides two variants of 'compile' method

public static Pattern compile(String regex)
Compiles the given regular expression into a pattern.

public static Pattern compile(String regex, int flags)
Compiles the given regular expression into a pattern with the given flags.

import java.util.regex.*;

public class PatternMatch {
    public static void main(String args[]){     
        Pattern p = Pattern.compile("a*b");
        
        Matcher m = p.matcher("aaaaab");
        System.out.println(m.matches());  
        
        m = p.matcher("aab");
        System.out.println(m.matches());  
        
         m = p.matcher("aba");
        System.out.println(m.matches());  
    }
}


Output
true
true
false


Prevoius                                                 Next                                                 Home

Match input against pattern

Patter class provide matches method to match the given input against a regular expression.

public static boolean matches(String regex, CharSequence input)
Compiles the given regular expression and attempts to match the given input against it.

import java.util.regex.*;

public class PatternMatch {
    public static void main(String args[]){       
        System.out.println(Pattern.matches("a*b", "b"));
        System.out.println(Pattern.matches("a*b", "ab"));
        System.out.println(Pattern.matches("a*b", "aab"));
        System.out.println(Pattern.matches("a*b", "aaabb"));
        System.out.println(Pattern.matches("a*b", "baa"));        
    }
}

Output
true
true
true
false
false

If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.

import java.util.regex.*;

public class PatternMatch {
    public static void main(String args[]){     
        Pattern p = Pattern.compile("a*b");
        
        Matcher m = p.matcher("aaaaab");
        System.out.println(m.matches());  
        
        m = p.matcher("aab");
        System.out.println(m.matches());  
        
         m = p.matcher("aba");
        System.out.println(m.matches());  
    }
}

Output
true
true
false


 
Prevoius                                                 Next                                                 Home