How to Validate SWIFT/BIC Code Using RegEx?
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.
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