Finance
US bank routing number shape Regex
A US routing number, also called an ABA number, has nine digits and identifies the bank in ACH payments and cheques. The first two digits fall into specific ranges: 00 to 12 for standard banks, 21 to 32 for thrift institutions, 61 to 72 for electronic transactions and 80 for traveller's cheques. This pattern checks the length and those prefix ranges, which catches many typos on direct deposit forms and payroll imports. It does not compute the weighted mod-10 checksum built into the last digit, and it cannot tell you whether a routing number is assigned to an active bank.
Pattern (JavaScript)
/^(?:0\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$/Starts at the start of the line/string, then (the character "0", then a digit (0-9) OR the character "1", then one of the characters 0-2 OR the character "2", then one of the characters 1-9 OR the character "3", then one of the characters 0-2 OR the character "6", then one of the characters 1-9 OR the character "7", then one of the characters 0-2 OR the text "80"), then exactly 7 digits, then the end of the line/string.
Limit: Checks the shape and prefix range only, not the mod-10 checksum or whether the bank exists.
Examples
Matches
- ✓ 021000000
- ✓ 110000000
- ✓ 322000000
- ✓ 800000000
Does not match
- ✗ 12345678
- ✗ 1234567890
- ✗ 500000000
- ✗ 13x000000
Try it
0 matches (JavaScript engine)
021000000
110000000
322000000
800000000
12345678
1234567890
500000000
13x000000Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:0\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$/g;
const text = "021000000";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:0\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$')
text = '021000000'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:0\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$/';
$text = '021000000';
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("^(?:0\\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\\d{7}$");
String text = "021000000";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:0\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$`)
text := "021000000"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:0\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$");
var text = "021000000";
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"^(?:0\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$").unwrap();
let text = "021000000";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:0\d|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}$/
text = '021000000'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are some prefixes like 50 rejected?
- The Federal Reserve only assigns certain ranges for the first two digits. Values such as 13 to 20 or 40 to 59 are not used, so rejecting them catches a large share of mistyped numbers.
- How does the routing number checksum work?
- The nine digits are multiplied by the repeating weights 3, 7 and 1, and the sum must be divisible by 10. That arithmetic needs code, so run it after this pattern matches.
- Is a routing number the same as an account number?
- No. The routing number identifies the bank, while the account number identifies your account at that bank. Account numbers vary in length and have no fixed national format.