Open In App

How to validate IFSC Code using Regular Expression

Improve
Improve
Like Article
Like
Save
Share
Report

Given string str, the task is to check whether the given string is a 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 are 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:
 

C++




// C++ program to validate the
// IFSC (Indian Financial System) Code using Regular Expression
#include <iostream>
#include <regex>
using namespace std;
 
// Function to validate the IFSC (Indian Financial System) Code.
bool isValidIFSCCode(string str)
{
 
  // Regex to check valid IFSC (Indian Financial System) Code.
  const regex pattern("^[A-Z]{4}0[A-Z0-9]{6}$");
 
 
  // If the IFSC (Indian Financial System) Code
  // is empty return false
  if (str.empty())
  {
     return false;
  }
 
  // Return true if the IFSC (Indian Financial System) Code
  // matched the ReGex
  if(regex_match(str, pattern))
  {
    return true;
  }
  else
  {
    return false;
  }
}
 
// Driver Code
int main()
{
   
  // Test Case 1:
  string str1 = "SBIN0125620";
  cout << boolalpha << isValidIFSCCode(str1) << endl;
 
  // Test Case 2:
  string str2 = "SBIN0125";
  cout << boolalpha << isValidIFSCCode(str2) << endl;
 
  // Test Case 3:
  string str3 = "1234SBIN012";
  cout << boolalpha << isValidIFSCCode(str3) << endl;
 
  // Test Case 4:
  string str4 = "SBIN7125620";
  cout << boolalpha <<isValidIFSCCode(str4) << endl;
 
  return 0;
}
 
// This code is contributed by yuvraj_chandra


Java




// 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 isValidIFSCCode(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(isValidIFSCCode(str1));
 
        // Test Case 2:
        String str2 = "SBIN0125";
        System.out.println(isValidIFSCCode(str2));
 
        // Test Case 3:
        String str3 = "1234SBIN012";
        System.out.println(isValidIFSCCode(str3));
 
        // Test Case 4:
        String str4 = "SBIN7125620";
        System.out.println(isValidIFSCCode(str4));
    }
}


Python3




# 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  isValidIFSCCode(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(isValidIFSCCode(str1))
 
# Test Case 2:
str2 = "SBIN0125"
print(isValidIFSCCode(str2))
 
# Test Case 3:
str3 = "1234SBIN012"
print(isValidIFSCCode(str3))
 
# Test Case 4:
str4 = "SBIN7125620"
print(isValidIFSCCode(str4))
 
# This code is contributed by avanitrachhadiya2155


C#




// C# program to validate IFSC
// Code using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG
{
 
  // Main Method
  static void Main(string[] args)
  {
 
    // Input strings to Match
    // valid IFSC Code
    string[] str = { "SBIN0125620", "SBIN0125",
                    "1234SBIN012", "SBIN7125620" };
    foreach(string s in str)
    {
      Console.WriteLine(isValidIFSCCode(s) ? "true"
                        : "false");
    }
    Console.ReadKey();
  }
 
  // method containing the regex
  public static bool isValidIFSCCode(string str)
  {
    string strRegex = @"^[A-Z]{4}0[A-Z0-9]{6}$";
    Regex re = new Regex(strRegex);
    if (re.IsMatch(str))
      return (true);
    else
      return (false);
  }
}
 
// This code is contributed by rahulchauhan2020model.


Javascript




// Javascript program to validate
// IFSC (Indian Financial System) Code using Regular Expression
 
// Function to validate the
// IFSC_Code 
function isValid_IFSC_Code(ifsc_Code)
{
 
    // Regex to check valid
    // ifsc_Code 
    let regex = new RegExp(/^[A-Z]{4}0[A-Z0-9]{6}$/);
 
    // if ifsc_Code
    // is empty return false
    if (ifsc_Code == null) {
        return "false";
    }
 
    // Return true if the ifsc_Code
    // matched the ReGex
    if (regex.test(ifsc_Code) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "SBIN0125620";
console.log(isValid_IFSC_Code(str1));
 
// Test Case 2:
let str2 = "SBIN0125";
console.log(isValid_IFSC_Code(str2));
 
// Test Case 3:
let str3 = "1234SBIN012";
console.log(isValid_IFSC_Code(str3));
 
// Test Case 4:
let str4 = "SBIN7125620";
console.log(isValid_IFSC_Code(str4));
 
// Test Case 5:
let str5 = "RAH12071998";
console.log(isValid_IFSC_Code(str5));
 
// This code is contributed by Rahul Chauhan


Output

true
false
false
false

Time Complexity: O(N) for each test case, where N is the length of the given string. 
Auxiliary Space: O(1)  

Using String.matches() method

This method tells whether or not this string matches the given regular expression. An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str). Pattern.compile(regex) compiles the pattern so that when you execute Matcher.matches(), the pattern is not recompiled again and again. Pattern.compile pre compiles it. However, if you use string.matches, it compiles the pattern every time you execute this line. So, it is better to use Pattern.compile().

C++




// C++ program to validate
// IFSC (Indian Financial System) Code
// using regular expression.
#include <iostream>
#include <regex>
using namespace std;
 
// Function to validate
// IFSC (Indian Financial System) Code
// using regular expression.
bool isValidIFSCode(string str)
{
   
    // Regex to check valid IFSC Code.
    string regex = "^[A-Z]{4}0[A-Z0-9]{6}$";
    return regex_match(str, std::regex(regex));
}
 
// Driver Code.
int main()
{
 
    // Test Case 1:
    string str1 = "SBIN0125620";
    cout << isValidIFSCode(str1) << endl;
 
    // Test Case 2:
    string str2 = "SBIN0125";
    cout << isValidIFSCode(str2) << endl;
 
    // Test Case 3:
    string str3 = "1234SBIN012";
    cout << isValidIFSCode(str3) << endl;
 
    // Test Case 4:
    string str4 = "SBIN7125620";
    cout << isValidIFSCode(str4) << endl;
 
    return 0;
}


Java




// Java program to validate
// IFSC (Indian Financial System) Code
// using regular expression.
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}$";
        return str.trim().matches(regex);
    }
 
    // 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));
    }
}


Python3




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}$"
    return bool(re.match(regex, str))
     
# Driver Code.
# Test Case 1:
str1 = "SBIN0125620"
if(isValidIFSCode(str1)):
    print('true')
else:
    print('false')
 
# Test Case 2:
str2 = "SBIN0125"
if(isValidIFSCode(str2)):
    print('true')
else:
    print('false')
 
# Test Case 3:
str3 = "1234SBIN012"
if(isValidIFSCode(str3)):
    print('true')
else:
    print('false')
 
# Test Case 4:
str4 = "SBIN7125620"
if(isValidIFSCode(str4)):
    print('true')
else:
    print('false')


C#




// C# program to validate
// IFSC (Indian Financial System) Code
// using regular expression.
using System;
using System.Text.RegularExpressions;
 
class Program {
    // Function to validate
    // IFSC (Indian Financial System) Code
    // using regular expression.
    static bool isValidIFSCode(string str)
    {
        // Regex to check valid IFSC Code.
        string regex = "^[A-Z]{4}0[A-Z0-9]{6}$";
        return Regex.IsMatch(str, regex);
    } // Driver Code.
    static void Main(string[] args)
    {
        // Test Case 1:
        string str1 = "SBIN0125620";
        Console.WriteLine(isValidIFSCode(str1));
 
        // Test Case 2:
        string str2 = "SBIN0125";
        Console.WriteLine(isValidIFSCode(str2));
 
        // Test Case 3:
        string str3 = "1234SBIN012";
        Console.WriteLine(isValidIFSCode(str3));
 
        // Test Case 4:
        string str4 = "SBIN7125620";
        Console.WriteLine(isValidIFSCode(str4));
    }
}


Javascript




function isValidIFSCode(str) {
  // Regex to check valid IFSC Code.
  let regex = /^[A-Z]{4}0[A-Z0-9]{6}$/;
  return str.trim().match(regex) != null;
}
 
// Test Case 1:
let str1 = "SBIN0125620";
console.log(isValidIFSCode(str1));
 
// Test Case 2:
let str2 = "SBIN0125";
console.log(isValidIFSCode(str2));
 
// Test Case 3:
let str3 = "1234SBIN012";
console.log(isValidIFSCode(str3));
 
// Test Case 4:
let str4 = "SBIN7125620";
console.log(isValidIFSCode(str4));


Output

true
false
false
false

Time Complexity: O(N) for each test case, where N is the length of the given string. 
Auxiliary Space: O(1)  



Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads