Open In App

How to Validate SWIFT/BIC Code Using RegEx?

Last Updated : 19 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. It is an alphanumeric code.
  2. Its length may vary from 8 to 11 characters.
  3. The first four letters should be from the alphabet.
  4. After the first four letters, the next two letters should be from the alphabet.
  5. The next two letters can be either from the alphabet or numbers.
  6. The last three letters should be in numeric form i.e. from 0 to 9.
  7. 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:

  1. Accept the string
  2. 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));


Output

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

  1. AAAABB11222
  2. AAAA-BB-11-222
  3. AAAABBCC222
  4. AAAA-BB-CC-222


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads