Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pattern Class and Pattern Object of Regular Expression

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 287
    Comment on it

    A compiled representation of an object which would be created after invoking one of it’s method of Pattern class is known as pattern object. These methods accept regular expression as an argument and matches with the given input. This class does not have any public constructor.

    Below are the pattern method to define the Pattern of Regex.

    1> Static Pattern Compile(String regex): This method compiles the given regex and return the instance of pattern.

    Eg: Pattern p = Pattern.compile(".p")

     

    2> Matcher matcher(CharSequence input): In this method user passes the character sequences as an argument and creates a matcher which will match the given input with regex pattern.

    Eg: Matcher m = p.matcher("ap");

     

    3>Static Boolean Matches(String regex, CharSequence input): In this method user passes regex and character sequences as an argument. When user invokes this method it compiles the regular expression and matches the given input with the pattern.

    Eg: boolean b2=Pattern.matches(".s","as");

     

    4> String[] split(CharSequence input): In this split method user passes the sequence of the character as an argument which splits the given input and matches with given pattern.

    import java.util.regex.*;
    
    public class RegexExample{
    	private static final String REGEX = "\\d";
        private static final String INPUT = "ten10eleven11twelve12thirteen";
    
        public static void main(String[] args) {
            Pattern p = Pattern.compile(REGEX);
            String[] items = p.split(INPUT);
            for(String s : items) {
                System.out.println(s);
    }
    
    }
    }
    

    Output: ten

                  eleven

                  twelve

                  Thirteen

    In the above code we have regex pattern is “\\d” means it matches any digit and when it will get the digit in the given string it split from there.

     

    5> String pattern(): This method returns the regex pattern and create the pattern object, After creating the pattern object we can use this object for invoking the matcher method and perform the match operation.

    import java.util.regex.*;
    
    
    public class RegexExample{
    public static void main(String args[]){
    Pattern p = Pattern.compile(".p");//. represents single character
    
    Matcher ma = p.matcher("ap");
    Matcher m = p.matcher("aaaappppp");
    boolean b = m.matches();
    boolean be = ma.matches();
    boolean b2=Pattern.matches(".s","as");
    
    	 	 	
    System.out.println(b);
    System.out.println(be);
    System.out.println(b2);
    
    }
    }
    

    Output: false

                   true

                   true

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: