- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 the ZIP code with Java Regular expressions
In order to match the ZIP code 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 which validates the ZIP code with Java Regular Expressions −
Example
public class Example { public static void main( String[] args ) { System.out.println(zipIndia("400709")); System.out.println(zipUS("10060")); } // validate zip public static boolean zipIndia( String z ) { return z.matches( "\d{6}" ); } public static boolean zipUS( String z ) { return z.matches( "\d{5}" ); } }
Output
true true
- Related Articles
- Zip Code validation using Java Regular Expressions
- Validate Phone with Java Regular Expressions
- Validate city and state with Java Regular Expressions
- Validate the first name and last name with Java Regular Expressions
- How to validate an email address using Java regular expressions.
- Search and Replace with Java regular expressions
- Java Regular Expressions Tutorial
- Working with Matcher.start() method in Java Regular Expressions
- Working with Matcher.end() method in Java Regular Expressions
- Working with simple groups in Java Regular Expressions
- Replace '*' with '^' with Java Regular Expressions
- Replace one string with another string with Java Regular Expressions
- Replace all words with another string with Java Regular Expressions
- Java regular expressions sample examples
- Possessive quantifiers Java Regular expressions

Advertisements