Mobile Number Pattern matches In Java



A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.
A typical invocation sequence is thus

 Pattern p = Pattern.compile("a*b");
 Matcher m = p.matcher("aaaaab");
 boolean b = m.matches();



 Example

import java.util.regex.*;  
class Demo
{
 public static void main(String[] args)
 {
  String  p1="[7-9]{1}[0-9]{9}";
  Pattern p = Pattern.compile(p1);
  Matcher m = p.matcher("9909556848");
  
  if( m.matches())
  {
   System.out.println("Match");
  }
  else
  {
   System.out.println("Not Match");
  }
 }
}


Out Put



EmoticonEmoticon