
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Validate Phone with Java Regular Expressions
In order to match the phone using regular expression, we use the matches method in Java. The java.lang.String.matches() method returns a boolean value which depends on the matching of the String with the regular expression.
Declaration − The java.lang.String.matches() method is declared as follows −
public boolean matches(String regex)
Let us see a program to validate a phone number with regular expressions −
Example
public class Example { public static void main( String[] args ) { System.out.println(phone("+91 1023456789")); } // validate zip public static boolean phone( String z ) { return z.matches("\+[0-9]*\s+\d{10}" ); // taking an assumption that a phone number is of ten digits } }
Output
true
Advertisements