skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, October 4, 2012

Regular expressions in Java

Posted by Admin at 12:35 PM – 0 comments
 
A regular expression is a pattern of characters that describes a set of strings. You can use the java.util.regex package to find, display, or modify some or all of the occurrences of a pattern in an input sequence. The simplest form of a regular expression is a literal string, such as "Java" or "programming." Regular expression matching also allows you to test whether a string fits into a specific syntactic form.

/*
should take only any of the following four pattrens:   
  
9999999999999  
999-999-999-9999  
999-999-9999  
9999999999  

*/

public static boolean validatePhoneNumber(String phNumber) {   
        boolean valResult = false;   
        String numPattern = "[0-9]{10}||[0-9]{13}||[0-9]{3}-[0-9]{3}-[0-9]{3}-[0-9]{4}||[0-9]{3}-[0-9]{3}-[0-9]{4}";   
        Matcher m;   
        m = Pattern.compile(numPattern).matcher(phNumber);   
        System.out.println(m.matches());   
        valResult = m.matches();   
        return valResult;   
} 

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger