How to validate pin code of India using Regular Expression | Java program to validate the pin code of India using Regular Expression . ; Function to validate the pin code of India . ; Regex to check valid pin code of India . ; Compile the ReGex ; If the pin code is empty return false ; Pattern class contains matcher ( ) method to find matching between given pin code and regular expression . ; Return if the pin code matched the ReGex ; Driver Code . ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
import java . util . regex . * ; class GFG { public static boolean isValidPinCode ( String pinCode ) { String regex = " ^ [ 1-9 ] { 1 } [ 0-9 ] { 2 } \\ s { 0,1 } [ 0-9 ] { 3 } $ " ; Pattern p = Pattern . compile ( regex ) ; if ( pinCode == null ) { return false ; } Matcher m = p . matcher ( pinCode ) ; return m . matches ( ) ; } public static void main ( String args [ ] ) { String num1 = "132103" ; System . out . println ( num1 + " : ▁ " + isValidPinCode ( num1 ) ) ; String num2 = "201 ▁ 305" ; System . out . println ( num2 + " : ▁ " + isValidPinCode ( num2 ) ) ; String num3 = "014205" ; System . out . println ( num3 + " : ▁ " + isValidPinCode ( num3 ) ) ; String num4 = "1473598" ; System . out . println ( num4 + " : ▁ " + isValidPinCode ( num4 ) ) ; } }