Monday 18 December 2017

How to write a Java Regex which matches any phone number | Java Regular Expressions | Regex in java


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=DwLcMtL03pU&index=48&list=UUhwKlOVR041tngjerWxVccw

RegexDemo.java

/**
 * Write a regular expression which matches any phone number
 */


public class RegexDemo
{
    public static void main(String[] args)
    {
        String regex = "\\d\\d\\d([-]){1}\\d\\d\\d([-]){1}\\d\\d\\d\\d";

        String phoneNum1 = "123-332-9999";
        System.out.println(phoneNum1.matches(regex));// true

        String phoneNum2 = "1233329999";
        System.out.println(phoneNum2.matches(regex));// false
    }

}


Output

true
false

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_phone_number.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_phone_number

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/ef02c9011c18b0aa670526ba89cc11f493b9b854/BasicJava/RegexDemo_phone_number/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment