How to validate IFSC Code using Regular Expression?


Indian Financial System Code is the abbreviation. Indian bank branches that take part in the electronic fund transfer system are identified by a special 11-character code. The Reserve Bank of India uses this code to transfer money between banks during internet transactions. There are two sections to the IFSC code. The bank is identified by the first four characters, whereas the branch is identified by the final six characters. NEFT (National Electronic Funds Transfer), RTGS (Real Time Gross Settlement), and IMPS (Immediate Payment Service) are just a few of the electronic transactions that require IFSC codes.

Method

Some general methods to validate an IFSC Code using Regular Expressions are −

  • Check for the correct length.

  • Check the first four characters.

  • Check the fifth character.

  • Check the last six characters .

Method 1: Check for the correct length

11 characters should make up the IFSC Code. To determine the length, use the following regular expression −

^.{11}$

This regular expression matches any 11 characters.

Syntax

To validate an IFSC code using regular expression, one can use the syntax to check for the correct length −

^([A-Z]{4}[0][A-Z0-9]{6})$ 
  • ^  Marks the beginning of the string

  • ([A-Z]{4}  Matches the first 4 characters of the IFSC code, which should be uppercase letters

  • [0]  Matches the fifth character of IFSC code, which should be a zero

  • [A-Z0-9]{6}  Matches the last 6 characters of IFSC code, which should be either uppercase letters or numbers.

  • $  Marks the end of the string

A total of 11 characters, including 4 uppercase letters, a zero, and then 6 uppercase letters or numerals, are guaranteed by this regular expression for the IFSC code.

Algorithm

Here is a detailed procedure for utilising regular expressions to validate an IFSC code's length −

Step 1 − Describe the regular expression pattern for an IFSC code: An IFSC code is an 11-character alphanumeric code. The bank code is represented by first four characters, branch code by the last six characters, and always-zero fifth character. An IFSC code's regular expression pattern is as follows−

[A-Z]{4}[0] [A-Z0-9]{6} $

Step 2 − Check regular expression pattern: Online regex testers like regex101.com and regexr.com can be used to test regular expression pattern. Enter the pattern into the tester, then enter an IFSC code to check if it matches the pattern.

Step 3 − Verify the IFSC code's length: Following the pattern test, you must verify the IFSC code's length. The len () method in Python can be used to determine whether an IFSC code is the required length of exactly 11 characters.

Step 4 − Use a regular expression pattern: After determining the length, you may use a regular expression pattern to determine whether the format of the IFSC code corresponds to what is expected. To apply the pattern to the IFSC code in Python, use the re module.

Example 1

The IFSC code in this case is verified using the regular expression [A-Z]40[A-Z0-9]6$. The following pattern is matched by the regular expression −

  • The first four letters of the code (from [A-Z]) must be uppercase.

  • The number zero (0) must be the fifth character.

  • The final six characters ([A-Z0-9]6$] can be capital letters or numerals.

The ifsc_code string and regular expression are matched using the regex_match function. The code is deemed to be valid if string matches the regular expression. It is invalid if anything else.

#include <iostream>
#include <regex>

using namespace std;

int main() {
   string ifsc_code = "SBIN0000123"; // Example IFSC code
   regex ifsc_regex("^[A-Z]{4}0[A-Z0-9]{6}$"); // Regular expression for IFSC code
    
   if (regex_match(ifsc_code, ifsc_regex)) {
      cout << "Valid IFSC code\n";
   } else {
      cout << "Invalid IFSC code\n";
   }
   return 0;
}

Output

Valid IFSC code

Method 2: Check the first four characters

The first four characters of IFSC Code identify the bank. One can use a regular expression to check that the first four characters are alphabets.

^[A-Z]{4} 

This regular expression matches any four uppercase alphabets.

Syntax

Here's a regular expression to check the first four characters of an IFSC Code −

^([A-Z]{4})

This regular expression uses the following syntax −

  • ^  Matches the start of the string.

  • [A-Z]  Matches any uppercase letter.

  • {4}  Specifies that the preceding pattern should occur exactly four times.

  • ()  Creates a capture group to extract the matched text.

This regular expression will match any string that starts with exactly four uppercase letters. To validate an entire IFSC code, one would need to check additional criteria beyond just the first four characters.

Algorithm

Here is a step-by-step algorithm for validating the first four characters of an IFSC code using a regular expression −

Step 1 − Specify regular expression pattern for IFSC code's first four characters. Only alphabets should be used in the first four characters, and the first two should stand in for bank code and other two for the location code, respectively. This can be expressed as [A-Z]4 in regular expression.

Step 2 − Obtain the input IFSC code that requires validation.

Step 3 − Remove the supplied IFSC code's first four characters.

Step 4 − Verify whether the extracted first four characters fit the specified pattern using the regular expression match () function. The input IFSC code is regarded as valid if the match is successful and the validation is successful. If there is no match, the validation is unsuccessful and the input IFSC code is deemed invalid.

Note: This algorithm only checks the first four characters of the IFSC code. The complete validation of the IFSC code requires additional checks for the remaining characters.

Example 2

In this illustration, the IFSC code we want to validate is represented by the string "ifsc_code." Then, in accordance with the IFSC code format, we build a regular expression pattern using the std::regex class that matches any string that begins with four letters.

The ifsc_code string is then checked to see if it matches the regular expression pattern using the std::regex_search function. If it does, a notice stating that the IFSC code is legitimate is output. If not, a notice stating that the IFSC code is invalid is output.

#include <iostream>
#include <regex>

int main() {
   std::string ifsc_code = "ABCD123456";
   std::regex pattern("^[A-Za-z]{4}");
  
   if (std::regex_search(ifsc_code, pattern)) {
      std::cout << "IFSC code is valid." << std::endl;
   } else {
      std::cout << "IFSC code is invalid." << std::endl;
   }
   return 0;
}

Output

IFSC code is valid.

Method 3: Check the fifth character

The fifth character of the IFSC Code is a zero (0) and is reserved for future use. One can use a regular expression to check that the fifth character is a zero.

^.{4}0

This regular expression matches any four characters followed by a zero.

Syntax

To check the fifth character and validate an IFSC code using a regular expression, you can use the following general syntax −

^[A-Z]{4}[0]{1}[A-Z0-9]{6}$
  • ^ and $  represent the start and end of the string, respectively, ensuring that the entire string matches the pattern.

  • [A-Z]{4}  matches exactly four uppercase alphabetic characters. This represents the bank code.

  • [0]{1} matches exactly one zero. This represents the fifth character in the IFSC code.

  • [A-Z0-9]{6} matches exactly six characters, which can be either uppercase alphabets or digits. This represents the branch code.

  • So overall, the pattern matches an IFSC code that starts with four uppercase alphabets, followed by a zero, and ends with six uppercase alphabets or digits.

Algorithm

Here is an algorithm that checks the fifth character of an IFSC code using a regular expression −

Step 1 − Input the IFSC code.

Step 2 − Define the regular expression pattern for IFSC codes: "^.{4}.{1}.*$"

Step 3 − Use the regular expression pattern to match the input IFSC code.

Step 4 − If there is a match −

  • Get the fifth character of the IFSC code.

  • Check if the fifth character is valid according to your criteria (e.g., a specific range of characters, specific characters, etc.).

  • If the fifth character is valid: - Output "IFSC code is valid."

  • If the fifth character is not valid: - Output "IFSC code is not valid."

Step 5 − If there is no match −

  • Output "IFSC code is not valid."

Example 3

An example in C++ showing how to utilise regular expressions without user input to check the fifth character of an IFSC code

The IFSC code "SBIN0001234" is used as a sample code in this example. To match the IFSC code structure, a regular expression pattern of [A-Za-z]40[A-Z0-9]6$ is employed. The fifth character is extracted and then validated if the code conforms to the pattern. It is accepted if the fifth character is an uppercase alphabetic character. It is invalid if anything else.

#include <iostream>
#include <regex>

int main() {
   std::string ifscCode = "SBIN0001234"; // Example IFSC code

   // Regular expression pattern to match IFSC code
   std::regex pattern("^[A-Za-z]{4}0[A-Z0-9]{6}$");

   // Check if the IFSC code matches the pattern
   if (std::regex_match(ifscCode, pattern)) {
      // Extract the fifth character
      char fifthCharacter = ifscCode[4];

      // Perform validation on the fifth character
      if (std::isalpha(fifthCharacter) && std::isupper(fifthCharacter)) {
         std::cout << "Fifth character is valid: " << fifthCharacter << std::endl;
      } else {
         std::cout << "Fifth character is invalid: " << fifthCharacter << std::endl;
      }
   } else {
      std::cout << "Invalid IFSC code." << std::endl;
   }
   return 0;
}

Output

Fifth character is invalid: 0

Method 4: Check the last six characters

The last six characters of the IFSC Code identify the branch. You can use a regular expression to check that the last six characters are alphanumeric.

^.{4}[A-Z0-9]{6}$

This regular expression matches any four characters followed by six alphanumeric characters.

By combining the above regular expressions, you can create a regular expression to validate the entire IFSC Code.

^[A-Z]{4}0[A-Z0-9]{6}$

This regular expression matches any valid IFSC Code.

Syntax

The regular expression pattern ^[A-Z]{4}\d{6}$ consists of the following components −

  • ^ indicates the start of the string.

  • [A-Z]{4} matches exactly four uppercase alphabetic characters.

  • \d{6} matches exactly six digits.

  • $ indicates the end of the string.

Algorithm

To check the last six characters of an IFSC code using regular expressions, you can follow this algorithm −

Step 1 − Define a regular expression pattern that matches the last six characters of an IFSC code. For example, the pattern could be "[A-Z0-9]{6}".

Step 2 − Create a list of samples IFSC codes for testing. These codes should be valid IFSC codes.

Step 3 − For each IFSC code in the list −

Extract the last six characters from the IFSC code.

Use the regular expression pattern to match the extracted characters.

If the match is successful, the last six characters are valid.

If the match fails, the last six characters are not valid.

Step 4 − Print the result (valid or not valid) for each IFSC code.

Example 4

Here, we define a regular expression pattern [A-Z0-9] $ that matches any set of uppercase letters (A-Z) or numbers (0-9), exactly six times (6), at end of string ($). Then, to see if ifscCode string matches pattern, we use std::regex_match(). In that case, we publish "IFSC code is valid," while in absence of that, we print "IFSC code invalid."

#include <iostream>
#include <regex>

int main() {
   std::string ifscCode = "SBIN0001234";  // Example IFSC code

   // Regular expression pattern to match the last six characters of an IFSC code
   std::regex pattern("[A-Z0-9]{6}$");

   // Checking if the last six characters of the IFSC code match the pattern
   if (std::regex_match(ifscCode, pattern)) {
      std::cout << "IFSC code is valid." << std::endl;
   } else {
      std::cout << "IFSC code is invalid." << std::endl;
   }
   return 0;
}

Output

IFSC code is invalid.

Conclusion

In conclusion, utilising regular expressions to validate an IFSC code can be a practical and effective technique to make sure the code is formatted correctly. Any input that does not adhere to the required pattern can be marked as invalid using a regular expression to define the pattern that the IFSC code must follow.

Prior to applying regular expressions to validate an IFSC code, it's critical to comprehend the format and structure of the code. The bank code is represented by the first four characters of the IFSC code, the branch code by the next six characters, and the zero as the fifth character.

Updated on: 01-Aug-2023

913 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements