How to validate IFSC Code using Regular Expression

Given string str, the task is to check whether the given string is valid IFSC (Indian Financial System) Code or not by using Regular Expression
The valid IFSC (Indian Financial System) Code must satisfy the following conditions: 

  1. It should be 11 characters long.
  2. The first four characters should be upper case alphabets.
  3. The fifth character should be 0.
  4. The last six characters usually numeric, but can also be alphabetic.

Examples: 

Input: str = “SBIN0125620”; 
Output: true 
Explanation: 
The given string satisfies all the above mentioned conditions. Therefore it is a valid IFSC (Indian Financial System) Code.
Input: str = “SBIN0125”; 
Output: false 
Explanation: 
The given string has 8 characters. Therefore it is not a valid IFSC (Indian Financial System) Code.
Input: str = “1234SBIN012”; 
Output: false 
Explanation: 
The given string doesn’t starts with alphabets. Therefore it is not a valid IFSC (Indian Financial System) Code.

Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer. 

  • Get the String.
  • Create a regular expression to check valid IFSC (Indian Financial System) Code as mentioned below: 
regex = "^[A-Z]{4}0[A-Z0-9]{6}$";

  • Where: 
    • ^ represents the starting of the string.
    • [A-Z]{4} represents the first four characters should be upper case alphabets.
    • 0 represents the fifth character should be 0.
    • [A-Z0-9]{6} represents the next six characters usually numeric, but can also be alphabetic.
    • $ represents the ending of the string.

Below is the implementation of the above approach:
 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to validate
// IFSC (Indian Financial System) Code
// using regular expression.
import java.util.regex.*;
class GFG {
 
    // Function to validate
    // IFSC (Indian Financial System) Code
    // using regular expression.
    public static boolean
    isValidIFSCode(String str)
    {
        // Regex to check valid IFSC Code.
        String regex
            = "^[A-Z]{4}0[A-Z0-9]{6}$";
 
        // Compile the ReGex
        Pattern p
            = Pattern.compile(regex);
 
        // If the string is empty
        // return false
        if (str == null) {
            return false;
        }
 
        // Pattern class contains matcher()
        // method to find matching between
        // the given string and
        // the regular expression.
        Matcher m = p.matcher(str);
 
        // Return if the string
        // matched the ReGex
        return m.matches();
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "SBIN0125620";
        System.out.println(isValidIFSCode(str1));
 
        // Test Case 2:
        String str2 = "SBIN0125";
        System.out.println(isValidIFSCode(str2));
 
        // Test Case 3:
        String str3 = "1234SBIN012";
        System.out.println(isValidIFSCode(str3));
 
        // Test Case 4:
        String str4 = "SBIN7125620";
        System.out.println(isValidIFSCode(str4));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to validate
# IFSC (Indian Financial System) Code 
# using regular expression
import re
 
# Function to validate
# IFSC (Indian Financial System) Code
# using regular expression.
def  isValidIFSCode(str):
 
    # Regex to check valid IFSC Code.
    regex = "^[A-Z]{4}0[A-Z0-9]{6}$"
     
    # Compile the ReGex
    p = re.compile(regex)
 
    # If the string is empty
    # return false
    if (str == None):
        return False
 
    # Return if the string
    # matched the ReGex
    if(re.search(p, str)):
        return True
    else:
        return False
 
# Driver code
 
# Test Case 1:
str1 = "SBIN0125620"
print(isValidIFSCode(str1))
 
# Test Case 2:
str2 = "SBIN0125"
print(isValidIFSCode(str2))
 
# Test Case 3:
str3 = "1234SBIN012"
print(isValidIFSCode(str3))
 
# Test Case 4:
str4 = "SBIN7125620"
print(isValidIFSCode(str4))
 
# This code is contributed by avanitrachhadiya2155

chevron_right


Output: 

true
false
false
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


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 : avanitrachhadiya2155