Sunday 2 November 2014

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

No comments:

Post a Comment