Regex to match SWIFT-BIC codes
A SWIFT-BIC number identifies a bank, not a particular bank account. The BIC part stands for Bank Identifier Code.
I had to look up the structure of SWIFT-BIC codes recently, and here it is:
- Four letters to identify the bank
- Two letters to identify the country
- Two letters or digits to identify the location
- Optionally, three letters or digits to identify a branch
Further details are given in the ISO 9362 standard.
We can use this as an example to illustrate several regular expression features, and how regular expressions are used in practice.
Regular expressionsIf your regular expression flavor supports listing a number of repetitions in braces, you could write the above format as
[A-Z]{6}[A-Z0-9]{2,5}
This would work, for example, with egrep but not with grep. YMMV.
That's concise, but a little too permissive. It allows anywhere from 2 to 5 alphanumeric characters on the end. But the standard says 2 or 5 alphanumeric characters after the country code, not between 2 and 5. For example, 3 characters after the country code would no be valid. So we could reduce our false positive rate a little by changing the regex to
[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$
Without the dollar sign on the end, ABCDEF12X would still match because the part of the regex up to the optional ([A-Z0-9]{3})? at the end would match at the beginning of the string. The dollar sign marks the end of the string, so it says the code has to end either after 8 or 11 characters and stop.
If your regex flavor does not support counts in braces, you could spell everything out:
[A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z0-9][A-Z0-9]([A-Z0-9]{3})?$Convenience versus accuracy
If you want to match only valid SWIFT-BIC codes, you can get perfect accuracy by checking against an exhaustive list of SWIFT-BIC codes. You could even write a regular expression that matches codes on this list and only codes on the list, but what would the point be? Regular expressions usually tradeoff convenience for accuracy.
I don't have a list of all valid SWIFT-BIC codes. If I did, it might be out of date by the time I download it. But if I'm trying to pull bank codes out of a text file, the regex
[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$
is likely to do a pretty good job. Regular expressions are usually used in a context where there's some tolerance for error. Maybe you use a regular expression to do a first pass, then weed out the mismatches with a manual review.
Capturing partsMaybe you want to do more than just find SWIFT codes. Maybe you want to look at their pieces.
For example, the fifth and sixth characters of a SWIFT code are the ISO 3166 two-letter abbreviation for the country the bank is in. (With one exception: XR represents Kosovo, which does not have an ISO 3166 code.)
You could replace
[A-Z]{6}
at the front of the regular expression with
[A-Z]{4}([A-Z]{2})
which will not change which strings match, but it will store the fifth and sixth characters as the first captured group. How you access captured group varies between various regular expression implementations.
LegibilityThe first proposed regular expression
[A-Z]{6}[A-Z0-9]{2,5}
is easy to read, at least in my opinion. It has grown over the course of this post to
[A-Z]{4}([A-Z]{2})[A-Z0-9]{2}([A-Z0-9]{3})?$
which is not as easy to read. This is typical: you start with a quick-and-dirty regular expression, the refine it until it meets your needs. Regular expressions tend to get uglier as they become more precise.
There are ways to make regular expressions more readable by using something like the /x modifier in Perl, which lets you insert white space and comments inside a regular expression.
That's nice, but it's also a little odd. If you're going to use a complicated regular expression in production code, then you should format it nicely and add comments. But then you have to ask why you're using a complicated regular expression in production code. I'm not saying this is never appropriate, but it's not the most common use case.
I could imagine using a simple regular expression when you want quick and dirty, and using an exhaustive list of SWIFT codes in production. A complex, well-commented regular expression seems to fall into a sort of no man's land in between.
The post Regex to match SWIFT-BIC codes first appeared on John D. Cook.