How to Validate SWIFT/BIC Code Using RegEx?
Last Updated :
19 Dec, 2022
A SWIFT/BIC code consists of 8-11 characters SWIFT follows a format that identifies your bank, country, location, and branch. A SWIFT code — is also called a BIC number.BIC is a standard format for Business Identifier Codes (BIC). It’s used to identify banks and financial institutions globally. These codes are used when transferring money between banks, in particular for international wire transfers or SEPA payments. Banks also use these codes to exchange messages with each other.
Format of SWIFT Code
A SWIFT/BIC is an 8-11 character code. That follows below architecture:
- It is an alphanumeric code.
- Its length may vary from 8 to 11 characters.
- The first four letters should be from the alphabet.
- After the first four letters, the next two letters should be from the alphabet.
- The next two letters can be either from the alphabet or numbers.
- The last three letters should be in numeric form i.e. from 0 to 9.
- It should not contain white spaces.
Example:
SWIFT_Code= AAAABB11222 OR AAAA-BB-11-222
Where,
- The first four letters represent the bank, which usually looks like an abbreviated version of the bank name
- The next two letters indicate the country where the bank is located.
- The next two letters/numbers indicate the location of the bank’s main office. (It can be numbers or letters)
- The next three letters identify a specific branch.
Approach
This problem can be solved using Regular Expression. Regex will validate the entered data and will provide the exact format. Below are steps that can be taken for the problem:
- Accept the string
- Create a regex pattern to validate the SWIFT code As written below:
regex="^[A-Z]{4}[-]{0,1}[A-Z]{2}[-]{0,1}[A-Z0-9]{2}[-]{0,1}[0-9]{3}$"
Where,
^ : Starting of the string
[A-Z]{4} : This expression will match 4 of the preceding items in the range form "A" to "Z".
[-]{0,1} : This expression will match one or zero preceding item if it is a hyphen symbol(-).
[A-Z0-9]{2] : This expression will match two of the preceding item in the range from "A" to "Z" and 0 to 9.
$ : Indicates the end of the string.
Code
Below is the code implementation of the above approach.
Java
// Java program to validate the
// SWIFT Code using Regular Expression
import java.util.regex.*;
class GFG
{
// Function to validate the
// SWIFT Code
public static boolean isValid_SWIFT_Code(String swift_code)
{
// Regex to check valid ISIN Code
String regex = "^[A-Z]{4}[-]{0,1}[A-Z]{2}[-]{0,1}[A-Z0-9]{2}[-]{0,1}[0-9]{3}$";
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the swift_code
// is empty return false
if (swift_code == null)
{
return false;
}
// Pattern class contains matcher() method
// to find matching between given
// swift_code using regular expression.
Matcher m = p.matcher(swift_code);
// Return if the swift_code
// matched the ReGex
return m.matches();
}
// Driver Code.
public static void main(String args[])
{
// Test Case 1:
String str1 = "AAAABB11222";
System.out.println("IS "+str1+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str1));
// Test Case 2:
String str2 = "AAAA-BB-11-222";
System.out.println("IS "+str2+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str2));
// Test Case 3:
String str3 = "@US-12345";
System.out.println("IS "+str3+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str3));
// Test Case 4:
String str4 = "XS9136812895";
System.out.println("IS "+str4+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str4));
// Test Case 5:
String str5 = "US45256BAD38";
System.out.println("IS "+str5+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str5));
// Test Case 6:
String str6 = "AAAA-BB-RR-222";
System.out.println("IS "+str6+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str6));
}
}
C++
// C++ program to validate the
// SWIFT Code using Regular
// Expression
#include <iostream>
#include <regex>
using namespace std;
// Function to validate the
// SWIFT Code
bool isValid_SWIFT_Code(string swift_code)
{
// Regex to check valid
// SWIFT Code.
const regex pattern("^[A-Z]{4}[-]{0,1}[A-Z]{2}[-]{0,1}[A-Z0-9]{2}[-]{0,1}[0-9]{3}$");
// If the swift_code
// is empty return false
if (swift_code.empty()) {
return false;
}
// Return true if the swift_code
// matched the ReGex
if (regex_match(swift_code, pattern))
{
return true;
}
else
{
return false;
}
}
// Driver Code
int main()
{
// Test Case 1:
string str1 = "AAAABB11222";
cout << isValid_SWIFT_Code(str1) << endl;
// Test Case 2:
string str2 = "AAAA-BB-11-222";
cout << isValid_SWIFT_Code(str2) << endl;
// Test Case 3:
string str3 = "@US-12345";
cout << isValid_SWIFT_Code(str3) << endl;
// Test Case 4:
string str4 = "XS9136812895";
cout << isValid_SWIFT_Code(str4) << endl;
// Test Case 5:
string str5 = "US45256BAD38";
cout << isValid_SWIFT_Code(str5) << endl;
// Test Case 6:
string str6 = "AAAA-BB-RR-222";
cout << isValid_SWIFT_Code(str6) << endl;
return 0;
}
Python3
# Python3 program to validate
# SWIFT Code using Regular Expression
import re
# Function to validate
# SWIFT Code
def isValid_SWIFT_Code(str):
# Regex to check valid SWIFT Code
regex = "^[A-Z]{4}[-]{0,1}[A-Z]{2}[-]{0,1}[A-Z0-9]{2}[-]{0,1}[0-9]{3}$"
# 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 = "AAAABB11222"
print(isValid_SWIFT_Code(str1))
# Test Case 2:
str2 = "AAAA-BB-11-222"
print(isValid_SWIFT_Code(str2))
# Test Case 3:
str3 = "@US-12345"
print(isValid_SWIFT_Code(str3))
# Test Case 4:
str4 = "XS9136812895"
print(isValid_SWIFT_Code(str4))
# Test Case 5:
str5 = "US45256BAD38"
print(isValid_SWIFT_Code(str5))
# Test Case 6:
str6 = "AAAA-BB-RR-222"
print(isValid_SWIFT_Code(str6))
C#
// C# program to validate the
// SWIFT Code using Regular Expression
using System;
using System.Text.RegularExpressions;
class GFG
{
// Function to validate the
// SWIFT Code
public static bool isValid_SWIFT_Code(string swift_code)
{
// Regex to check valid ISIN Code
string regex = "^[A-Z]{4}[-]{0,1}[A-Z]{2}[-]{0,1}[A-Z0-9]{2}[-]{0,1}[0-9]{3}$";
// Compile the ReGex
Regex p = new Regex(regex);
// If the swift_code
// is empty return false
if (swift_code == null)
{
return false;
}
// Pattern class contains matcher() method
// to find matching between given
// swift_code using regular expression.
Match m = p.Match(swift_code);
// Return if the swift_code
// matched the ReGex
return m.Success;
}
// Driver Code.
public static void Main()
{
// Test Case 1:
string str1 = "AAAABB11222";
Console.WriteLine("IS "+str1+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str1));
// Test Case 2:
string str2 = "AAAA-BB-11-222";
Console.WriteLine("IS "+str2+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str2));
// Test Case 3:
string str3 = "@US-12345";
Console.WriteLine("IS "+str3+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str3));
// Test Case 4:
string str4 = "XS9136812895";
Console.WriteLine("IS "+str4+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str4));
// Test Case 5:
string str5 = "US45256BAD38";
Console.WriteLine("IS "+str5+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str5));
// Test Case 6:
string str6 = "AAAA-BB-RR-222";
Console.WriteLine("IS "+str6+" matches with valid SWIFT Code? "+isValid_SWIFT_Code(str6));
}
}
// This code is contributed by Pushpesh Raj.
JavaScript
// Javascript program to validate
// SWIFT Code using Regular Expression
// Function to validate the
// SWIFT Code
function isValid_SWIFT_Code(swift_code) {
// Regex to check valid
// SWIFT CODE
let regex = new RegExp(/^[A-Z]{4}[-]{0,1}[A-Z]{2}[-]{0,1}[A-Z0-9]{2}[-]{0,1}[0-9]{3}$/);
// SWIFT CODE
// is empty return false
if (swift_code == null) {
return "false";
}
// Return true if the swift_code
// matched the ReGex
if (regex.test(swift_code) == true) {
return "true";
}
else {
return "false";
}
}
// Driver Code
// Test Case 1:
let str1 = "AAAABB11222";
console.log(isValid_SWIFT_Code(str1));
// Test Case 2:
let str2 = "AAAA-BB-11-222";
console.log(isValid_SWIFT_Code(str2));
// Test Case 3:
let str3 = "@US-12345";
console.log(isValid_SWIFT_Code(str3));
// Test Case 4:
let str4 = "XS9136812895";
console.log(isValid_SWIFT_Code(str4));
// Test Case 5:
let str5 = "US45256BAD38";
console.log(isValid_SWIFT_Code(str5));
// Test Case 6:
let str6 = "AAAA-BB-RR-222";
console.log(isValid_SWIFT_Code(str6));
OutputIS AAAABB11222 matches with valid SWIFT Code? true
IS AAAA-BB-11-222 matches with valid SWIFT Code? true
IS @US-12345 matches with valid SWIFT Code? false
IS XS9136812895 matches with valid SWIFT Code? false
IS US45256BAD38 matches with valid SWIFT Code? false
IS AAAA-BB-RR-222 matches with valid SWIFT Code? true
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
So by the above discussion, We can sum up the correct SWIFT code format given below:
- AAAABB11222
- AAAA-BB-11-222
- AAAABBCC222
- AAAA-BB-CC-222
Similar Reads
How to validate an IP address using ReGex
Given an IP address, the task is to validate this IP address with the help of Regex (Regular Expression) in C++ as a valid IPv4 address or IPv6 address. If the IP address is not valid then print an invalid IP address. Examples: Input: str = "203.120.223.13" Output: Valid IPv4 Input: str = "000.12.23
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 IFSC Code using Regular Expression
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
8 min read
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
5 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 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
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
Validate GIT Repository using Regular Expression
GIT stands for GLOBAL INFORMATION TRACKER.Given some Git repositories, the task is to check if they are valid or not using regular expressions. Rules for the valid Git Repository are: It is an alphanumeric string containing uppercase Alphabet letters(A-Z) and digits(0-9).It should not contain any wh
5 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 GST (Goods and Services Tax) number using Regular Expression
Given string str, the task is to check whether the given string is a valid GST (Goods and Services Tax) number or not using Regular Expression. The valid GST (Goods and Services Tax) number must satisfy the following conditions: It should be 15 characters long.The first 2 characters should be a numb
6 min read