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
import java.util.regex.*;
class GFG
{
public static boolean isValid_SWIFT_Code(String swift_code)
{
String regex = "^[A-Z]{4}[-]{0,1}[A-Z]{2}[-]{0,1}[A-Z0-9]{2}[-]{0,1}[0-9]{3}$" ;
Pattern p = Pattern.compile(regex);
if (swift_code == null )
{
return false ;
}
Matcher m = p.matcher(swift_code);
return m.matches();
}
public static void main(String args[])
{
String str1 = "AAAABB11222" ;
System.out.println( "IS " +str1+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str1));
String str2 = "AAAA-BB-11-222" ;
System.out.println( "IS " +str2+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str2));
String str3 = "@US-12345" ;
System.out.println( "IS " +str3+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str3));
String str4 = "XS9136812895" ;
System.out.println( "IS " +str4+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str4));
String str5 = "US45256BAD38" ;
System.out.println( "IS " +str5+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str5));
String str6 = "AAAA-BB-RR-222" ;
System.out.println( "IS " +str6+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str6));
}
}
|
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValid_SWIFT_Code(string 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 (swift_code.empty()) {
return false ;
}
if (regex_match(swift_code, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "AAAABB11222" ;
cout << isValid_SWIFT_Code(str1) << endl;
string str2 = "AAAA-BB-11-222" ;
cout << isValid_SWIFT_Code(str2) << endl;
string str3 = "@US-12345" ;
cout << isValid_SWIFT_Code(str3) << endl;
string str4 = "XS9136812895" ;
cout << isValid_SWIFT_Code(str4) << endl;
string str5 = "US45256BAD38" ;
cout << isValid_SWIFT_Code(str5) << endl;
string str6 = "AAAA-BB-RR-222" ;
cout << isValid_SWIFT_Code(str6) << endl;
return 0;
}
|
Python3
import re
def isValid_SWIFT_Code( str ):
regex = "^[A-Z]{4}[-]{0,1}[A-Z]{2}[-]{0,1}[A-Z0-9]{2}[-]{0,1}[0-9]{3}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "AAAABB11222"
print (isValid_SWIFT_Code(str1))
str2 = "AAAA-BB-11-222"
print (isValid_SWIFT_Code(str2))
str3 = "@US-12345"
print (isValid_SWIFT_Code(str3))
str4 = "XS9136812895"
print (isValid_SWIFT_Code(str4))
str5 = "US45256BAD38"
print (isValid_SWIFT_Code(str5))
str6 = "AAAA-BB-RR-222"
print (isValid_SWIFT_Code(str6))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
public static bool isValid_SWIFT_Code( string swift_code)
{
string regex = "^[A-Z]{4}[-]{0,1}[A-Z]{2}[-]{0,1}[A-Z0-9]{2}[-]{0,1}[0-9]{3}$" ;
Regex p = new Regex(regex);
if (swift_code == null )
{
return false ;
}
Match m = p.Match(swift_code);
return m.Success;
}
public static void Main()
{
string str1 = "AAAABB11222" ;
Console.WriteLine( "IS " +str1+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str1));
string str2 = "AAAA-BB-11-222" ;
Console.WriteLine( "IS " +str2+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str2));
string str3 = "@US-12345" ;
Console.WriteLine( "IS " +str3+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str3));
string str4 = "XS9136812895" ;
Console.WriteLine( "IS " +str4+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str4));
string str5 = "US45256BAD38" ;
Console.WriteLine( "IS " +str5+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str5));
string str6 = "AAAA-BB-RR-222" ;
Console.WriteLine( "IS " +str6+ " matches with valid SWIFT Code? " +isValid_SWIFT_Code(str6));
}
}
|
Javascript
function isValid_SWIFT_Code(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}$/);
if (swift_code == null ) {
return "false" ;
}
if (regex.test(swift_code) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "AAAABB11222" ;
console.log(isValid_SWIFT_Code(str1));
let str2 = "AAAA-BB-11-222" ;
console.log(isValid_SWIFT_Code(str2));
let str3 = "@US-12345" ;
console.log(isValid_SWIFT_Code(str3));
let str4 = "XS9136812895" ;
console.log(isValid_SWIFT_Code(str4));
let str5 = "US45256BAD38" ;
console.log(isValid_SWIFT_Code(str5));
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:
- AAAABB11222
- AAAA-BB-11-222
- AAAABBCC222
- AAAA-BB-CC-222
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...