Finance
IBAN (international bank account number) Regex
An IBAN begins with a two letter country code and two check digits, followed by a country-specific account section of up to 30 characters. This pattern accepts that general shape in either the compact electronic form or the printed form with spaces every four characters. It is a good first filter for payment forms, invoice parsers and supplier onboarding sheets that receive bank details from many countries. What it cannot do is confirm that the length matches the country, or run the mod-97 check digit calculation. Pair it with a proper IBAN library before you send money anywhere.
Pattern (JavaScript)
/^[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?$/Starts at the start of the line/string, then an uppercase letter, exactly 2 times, then exactly 2 digits, then (a space, optionally (zero or one time), then one of the characters A-Z0-9, exactly 4 times), between 2 and 7 times, then (a space, optionally (zero or one time), then one of the characters A-Z0-9, between 1 and 3 times), optionally (zero or one time), then the end of the line/string.
Limit: Checks the shape only, not the mod-97 check digits or the per-country length.
Examples
Matches
- ✓ GB82 WEST 1234 5698 7654 32
- ✓ DE89370400440532013000
- ✓ FR14 2004 1010 0505 0001 3M02 606
Does not match
- ✗ gb82 west 1234 5698 7654 32
- ✗ GB82
- ✗ 1234 5678 9012 3456
- ✗ GB82-WEST-1234-5698
Try it
0 matches (JavaScript engine)
GB82 WEST 1234 5698 7654 32
DE89370400440532013000
FR14 2004 1010 0505 0001 3M02 606
gb82 west 1234 5698 7654 32
GB82
1234 5678 9012 3456
GB82-WEST-1234-5698Code 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]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?$/g;
const text = "GB82 WEST 1234 5698 7654 32";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?$')
text = 'GB82 WEST 1234 5698 7654 32'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?$/';
$text = 'GB82 WEST 1234 5698 7654 32';
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]{2}\\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?$");
String text = "GB82 WEST 1234 5698 7654 32";
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]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?$`)
text := "GB82 WEST 1234 5698 7654 32"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?$");
var text = "GB82 WEST 1234 5698 7654 32";
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]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?$").unwrap();
let text = "GB82 WEST 1234 5698 7654 32";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?$/
text = 'GB82 WEST 1234 5698 7654 32'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does this IBAN regex check the country length?
- No. Each country has a fixed IBAN length, for example 22 characters for Germany and 27 for France. Checking that would need a table of countries, which belongs in code rather than in one regex.
- What are the two digits after the country code?
- They are check digits computed with the mod-97 algorithm over the whole IBAN. A regex can confirm they are digits but cannot recompute them, so a single typo elsewhere still passes this pattern.
- Should I accept lowercase IBANs?
- The standard uses uppercase letters, which is why this pattern is case-sensitive. If users paste lowercase values, convert the input to uppercase before testing it rather than adding the i flag.
Related patterns
- 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})$
- Common ISO 4217 currency code^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$