How to validate IFSC Code using Regular Expression
Last Updated :
14 Mar, 2023
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:
- It should be 11 characters long.
- The first four characters should be upper case alphabets.
- The fifth character should be 0.
- 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++
#include <iostream>
#include <regex>
using namespace std;
bool isValidIFSCCode(string str)
{
const regex pattern( "^[A-Z]{4}0[A-Z0-9]{6}$" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "SBIN0125620" ;
cout << boolalpha << isValidIFSCCode(str1) << endl;
string str2 = "SBIN0125" ;
cout << boolalpha << isValidIFSCCode(str2) << endl;
string str3 = "1234SBIN012" ;
cout << boolalpha << isValidIFSCCode(str3) << endl;
string str4 = "SBIN7125620" ;
cout << boolalpha <<isValidIFSCCode(str4) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidIFSCCode(String str)
{
String regex = "^[A-Z]{4}0[A-Z0-9]{6}$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "SBIN0125620" ;
System.out.println(isValidIFSCCode(str1));
String str2 = "SBIN0125" ;
System.out.println(isValidIFSCCode(str2));
String str3 = "1234SBIN012" ;
System.out.println(isValidIFSCCode(str3));
String str4 = "SBIN7125620" ;
System.out.println(isValidIFSCCode(str4));
}
}
|
Python3
import re
def isValidIFSCCode( str ):
regex = "^[A-Z]{4}0[A-Z0-9]{6}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "SBIN0125620"
print (isValidIFSCCode(str1))
str2 = "SBIN0125"
print (isValidIFSCCode(str2))
str3 = "1234SBIN012"
print (isValidIFSCCode(str3))
str4 = "SBIN7125620"
print (isValidIFSCCode(str4))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str = { "SBIN0125620" , "SBIN0125" ,
"1234SBIN012" , "SBIN7125620" };
foreach ( string s in str)
{
Console.WriteLine(isValidIFSCCode(s) ? "true"
: "false" );
}
Console.ReadKey();
}
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 );
}
}
|
Javascript
function isValid_IFSC_Code(ifsc_Code)
{
let regex = new RegExp(/^[A-Z]{4}0[A-Z0-9]{6}$/);
if (ifsc_Code == null ) {
return "false" ;
}
if (regex.test(ifsc_Code) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "SBIN0125620" ;
console.log(isValid_IFSC_Code(str1));
let str2 = "SBIN0125" ;
console.log(isValid_IFSC_Code(str2));
let str3 = "1234SBIN012" ;
console.log(isValid_IFSC_Code(str3));
let str4 = "SBIN7125620" ;
console.log(isValid_IFSC_Code(str4));
let str5 = "RAH12071998" ;
console.log(isValid_IFSC_Code(str5));
|
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++
#include <iostream>
#include <regex>
using namespace std;
bool isValidIFSCode(string str)
{
string regex = "^[A-Z]{4}0[A-Z0-9]{6}$" ;
return regex_match(str, std::regex(regex));
}
int main()
{
string str1 = "SBIN0125620" ;
cout << isValidIFSCode(str1) << endl;
string str2 = "SBIN0125" ;
cout << isValidIFSCode(str2) << endl;
string str3 = "1234SBIN012" ;
cout << isValidIFSCode(str3) << endl;
string str4 = "SBIN7125620" ;
cout << isValidIFSCode(str4) << endl;
return 0;
}
|
Java
class GFG {
public static boolean isValidIFSCode(String str)
{
String regex = "^[A-Z]{4}0[A-Z0-9]{6}$" ;
return str.trim().matches(regex);
}
public static void main(String args[])
{
String str1 = "SBIN0125620" ;
System.out.println(isValidIFSCode(str1));
String str2 = "SBIN0125" ;
System.out.println(isValidIFSCode(str2));
String str3 = "1234SBIN012" ;
System.out.println(isValidIFSCode(str3));
String str4 = "SBIN7125620" ;
System.out.println(isValidIFSCode(str4));
}
}
|
Python3
import re
def isValidIFSCode( str ):
regex = "^[A-Z]{4}0[A-Z0-9]{6}$"
return bool (re.match(regex, str ))
str1 = "SBIN0125620"
if (isValidIFSCode(str1)):
print ( 'true' )
else :
print ( 'false' )
str2 = "SBIN0125"
if (isValidIFSCode(str2)):
print ( 'true' )
else :
print ( 'false' )
str3 = "1234SBIN012"
if (isValidIFSCode(str3)):
print ( 'true' )
else :
print ( 'false' )
str4 = "SBIN7125620"
if (isValidIFSCode(str4)):
print ( 'true' )
else :
print ( 'false' )
|
C#
using System;
using System.Text.RegularExpressions;
class Program {
static bool isValidIFSCode( string str)
{
string regex = "^[A-Z]{4}0[A-Z0-9]{6}$" ;
return Regex.IsMatch(str, regex);
}
static void Main( string [] args)
{
string str1 = "SBIN0125620" ;
Console.WriteLine(isValidIFSCode(str1));
string str2 = "SBIN0125" ;
Console.WriteLine(isValidIFSCode(str2));
string str3 = "1234SBIN012" ;
Console.WriteLine(isValidIFSCode(str3));
string str4 = "SBIN7125620" ;
Console.WriteLine(isValidIFSCode(str4));
}
}
|
Javascript
function isValidIFSCode(str) {
let regex = /^[A-Z]{4}0[A-Z0-9]{6}$/;
return str.trim().match(regex) != null ;
}
let str1 = "SBIN0125620" ;
console.log(isValidIFSCode(str1));
let str2 = "SBIN0125" ;
console.log(isValidIFSCode(str2));
let str3 = "1234SBIN012" ;
console.log(isValidIFSCode(str3));
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)
Similar Reads
How to Validate MICR Code using Regular Expression?
MICR stands for Magnetic Ink Character Recognition. This technology provides transaction security, ensuring the correctness of bank cheques. MICR code makes cheque processing faster and safer. MICR Technology reduces cheque-related fraudulent activities. Structure of a Magnetic Ink Character Recogni
6 min read
How to validate ISIN using Regular Expressions
ISIN stands for International Securities Identification Number. Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must sati
6 min read
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. It can be only six digits.It should not start with zero.First digit of the pin cod
6 min read
How to validate MAC address using Regular Expression
Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression. A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits.One way to represent them is to form six pairs of the characters separated with
6 min read
How to validate CVV number using Regular Expression
Given string str, the task is to check whether it is a valid CVV (Card Verification Value) number or not by using Regular Expression. The valid CVV (Card Verification Value) number must satisfy the following conditions: It should have 3 or 4 digits.It should have a digit between 0-9.It should not ha
5 min read
Regular Expressions to Validate ISBN Code
Given some ISBN Codes, the task is to check if they are valid or not using regular expressions. Rules for the valid codes are: It is a unique 10 or 13-digit.It may or may not contain a hyphen.It should not contain whitespaces and other special characters.It does not allow alphabet letters. Examples:
5 min read
How to validate HTML tag using Regular Expression
Given string str, the task is to check whether it is a valid HTML tag or not by using Regular Expression.The valid HTML tag must satisfy the following conditions: It should start with an opening tag (<).It should be followed by a double quotes string or single quotes string.It should not allow on
6 min read
How to validate PAN Card number using Regular Expression
Given string str of alphanumeric characters, the task is to check whether the string is a valid PAN (Permanent Account Number) Card number or not by using Regular Expression.The valid PAN Card number must satisfy the following conditions: It should be ten characters long.The first five characters sh
6 min read
How to validate a domain name using Regular Expression
Given string str, the task is to check whether the given string is a valid domain name or not by using Regular Expression.The valid domain name must satisfy the following conditions: The domain name should be a-z or A-Z or 0-9 and hyphen (-).The domain name should be between 1 and 63 characters long
6 min read
How to validate Visa Card number using Regular Expression
Given a string str, the task is to check whether the given string is a valid Visa Card number or not by using Regular Expression. The valid Visa Card number must satisfy the following conditions: It should be 13 or 16 digits long, new cards have 16 digits and old cards have 13 digits.It should start
6 min read