Finance
SWIFT / BIC code Regex
A SWIFT or BIC code identifies a bank in international wire transfers. It has four letters for the institution, two letters for the country, two letters or digits for the location, and an optional three character branch code, giving a total of 8 or 11 characters. This pattern checks exactly that structure, which makes it useful on payment instruction forms, supplier records and bank statement parsers. It is deliberately case-sensitive because codes are published in uppercase. It does not know whether the country code is a real ISO country or whether the bank exists in the SWIFT directory.
Pattern (JavaScript)
/^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$/Starts at the start of the line/string, then an uppercase letter, exactly 4 times, then an uppercase letter, exactly 2 times, then one of the characters A-Z0-9, exactly 2 times, then (one of the characters A-Z0-9, exactly 3 times), optionally (zero or one time), then the end of the line/string.
Limit: Checks the format only, not whether the bank or branch exists.
Examples
Matches
- ✓ DEUTDEFF
- ✓ NWBKGB2L
- ✓ TESTUS33XXX
- ✓ BOFAUS3N
Does not match
- ✗ DEUTDEF
- ✗ DEUT DE FF
- ✗ 1EUTDEFF
- ✗ deutdeff
Try it
0 matches (JavaScript engine)
DEUTDEFF
NWBKGB2L
TESTUS33XXX
BOFAUS3N
DEUTDEF
DEUT DE FF
1EUTDEFF
deutdeffCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$/g;
const text = "DEUTDEFF";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$')
text = 'DEUTDEFF'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$/';
$text = 'DEUTDEFF';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$");
String text = "DEUTDEFF";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$`)
text := "DEUTDEFF"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$");
var text = "DEUTDEFF";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$").unwrap();
let text = "DEUTDEFF";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$/
text = 'DEUTDEFF'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- What is the difference between SWIFT and BIC?
- They are the same thing. BIC stands for Business Identifier Code, the official ISO 9362 name, while SWIFT is the network that uses it. Forms often label the field with both names.
- What does XXX at the end mean?
- An 11 character code ending in XXX points to the primary office of the bank. It is equivalent to the 8 character code with no branch, so both forms match this pattern.
- Can the location code contain digits?
- Yes. The two location characters may be letters or digits, and a digit 0 in the second position traditionally marks a test code. The pattern allows letters and digits there for that reason.
Related patterns
- US bank routing number shape^(?:0\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$
- Accounting negative amount in parentheses^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$
- American Express card number shape^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$
- Bitcoin address shape^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$