- 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
Java Program to Match Zip Codes
Let’s say we have the following zip code.
String zipStr = "12345";
Now, set the following regular expression to match zip codes in America.
String reg = "^[0-9]{5}(?:-[0-9]{4})?$";
Example
public class Demo { public static void main(String[] args) { String zipStr = "12345"; // regular expression String reg = "^[0-9]{5}(?:-[0-9]{4})?$"; boolean res = zipStr.matches(reg); System.out.println("Is it a valid zip code in US? "+res); } }
Output
Is it a valid zip code in US? True
Advertisements