How to validate pin code of India using Regular Expression

Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression.

The valid pin code of India must satisfy the following conditions.

  1. It can be only six digits.
  2. It should not start with zero.
  3. First digit of the pin code must be from 1 to 9.
  4. Next five digits of the pin code may range from 0 to 9.
  5. It should allow only one white space, but after three digits, although this is optional.

Examples:

Input: num = “132103”
Output: true
Explanation:
The given number satisfies all the above mentioned conditions.

Input: num = “201 305”
Output: true
Explanation:
The given number satisfies all the above mentioned conditions.



Input: num = “014205”
Output: false
Explanation:
The given number start with zero, therefore it is not a valid pin code of India.

Input: num = “1473598”
Output: false
Explanation:
The given number contains seven digits, therefore it is not a valid pin code of India.

Approach: This problem can be used by using Regular Expression.

  1. Get the number.
  2. Create a regular expression to validate pin code of india as mentioned below:
     
    regex = "^[1-9]{1}[0-9]{2}\\s{0, 1}[0-9]{3}$";
    

    Where:

    • ^ represents the starting of the number.
    • [1-9]{1} represents the starting digit in the pin code ranging from 1 to 9.
    • [0-9]{2} represents the next two digits in the pin code ranging from 0 to 9.
    • \\s{0, 1} represents the white space in the pin code that can occur once or never.
    • [0-9]{3} represents the last three digits in the pin code ranging from 0 to 9.
    • $ represents the ending of the number.
  3. Match the given number with the regex, in Java, this can be done by using Pattern.matcher().
  4. Return true if the number matches with the given regex, else return false.

Below is the implementation of the above approach.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to validate the  
# pin code of India using Regular 
# Expression. 
import re
  
# Function to validate the pin code
# of India. 
def isValidPinCode(pinCode):
      
    # Regex to check valid pin code
    # of India.
    regex = "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$"
  
    # Compile the ReGex 
    p = re.compile(regex);
      
    # If the pin code is empty
    # return false
    if (pinCode == ''):
        return False;
          
    # Pattern class contains matcher() method
    # to find matching between given pin code
    # and regular expression.
    m = re.match(p, pinCode);
      
    # Return True if the pin code
    # matched the ReGex else False
    if m is None:
        return False
    else:
        return True
  
# Driver code 
if __name__ == "__main__":
      
    # Test case 1
    num1 = "132103";
    print(num1, ": ", isValidPinCode(num1));
      
    # Test case 2:
    num2 = "201 305";
    print(num2, ": ", isValidPinCode(num2));
      
    # Test case 3:
    num3 = "014205";
    print(num3, ": ", isValidPinCode(num3));
      
    # Test case 4:
    num4 = "1473598";
    print(num4, ": ", isValidPinCode(num4));
      
# This code is contributed by AnkitRai01

chevron_right


Output:

132103: true
201 305: true
014205: false
1473598: false

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

External Technical Content Reviewer at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01