Skip to content

Java RegEx – Validate Indian Pincode

Java RegEx – Validate Indian Pincode example shows how to validate an Indian pincode using regex in Java. An Indian pincode is a 6 digit number that can be validated using a regex pattern.

How to validate Indian pincode using regex in Java?

Before we dive into validating a pincode, it is necessary to understand what is the structure and rules of any pincode number. Once we know the structure and rules, we can easily write a regex pattern to validate that.

An Indian Pin is a short form of Postal Index Number and popularly referred to as a PIN code. It is a 6 digit number that is used by the Indian post to simplify the sorting and distribution of the posts. The first digit in the pin code represents zone, the second digit is for sub-zone, and the third digit is the district. The last three digits represent the post offices within the area. The zone (the first digit) is never a zero. The rest of the digits can be zero.

Since we have all the rules in place now, here is the regex pattern for that.


Let’s try some test pin codes against this pattern to check.


The first test pin code starts with a zero so it is invalid. The second value has only 5 digits so it is invalid as well. The third value is 7 in length, so it failed. The fourth value has a character “a” that is not allowed inside the Pincode. The rest of the codes are valid.


Sometimes a PIN code is mentioned with space after 3 digits like “123 456”. In that case, you can either remove the space from it using the replace method before validation or you can use the below-given pattern that allows pin codes with space.

If you want to learn more about the regex, please visit the Java regex tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.