Sunday 2 November 2014

lookingAt(): Match input sequence

public boolean lookingAt()
Returns true true if, and only if, a prefix of the input sequence matches this matcher's pattern. Unlike matches method, it does not require that the entire region be matched.

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 = br.readLine();
        pattern = Pattern.compile(regex);
        
        while(true){
            try{
                System.out.println("Enter the string");
                text = br.readLine();
                matcher = pattern.matcher(text);
                
                System.out.println("Is pattern matched : " + matcher.lookingAt());
                System.out.println();
            }
            catch(IOException e){
                System.out.println(e);
            }
        }     
    }
}

Sample Output
Enter Regular Expression
a*b
Enter the string
abb
Is pattern matched : true

Enter the string
abbbb
Is pattern matched : true

Enter the string
xb
Is pattern matched : false




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment