Finance
Common ISO 4217 currency code Regex
ISO 4217 assigns three letter codes to currencies, such as USD for the US dollar and JPY for the Japanese yen. Instead of accepting any three capital letters, this pattern lists twenty widely traded codes, so a typo like USB or a made-up code like XYZ is rejected. That makes it practical for currency dropdown checks, forex data imports and invoice parsers that only need major currencies. The trade-off is that the list is short by design. It will reject legitimate but less common codes, so extend the alternation with the currencies your business actually handles.
Pattern (JavaScript)
/^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$/Starts at the start of the line/string, then (the text "USD" OR the text "EUR" OR the text "GBP" OR the text "JPY" OR the text "CHF" OR the text "CAD" OR the text "AUD" OR the text "NZD" OR the text "CNY" OR the text "HKD" OR the text "SGD" OR the text "INR" OR the text "AED" OR the text "SAR" OR the text "SEK" OR the text "NOK" OR the text "DKK" OR the text "ZAR" OR the text "BRL" OR the text "MXN"), then the end of the line/string.
Limit: Covers a fixed list of twenty common codes, not the full ISO 4217 table.
Examples
Matches
- ✓ USD
- ✓ EUR
- ✓ JPY
- ✓ AED
Does not match
- ✗ usd
- ✗ XYZ
- ✗ US$
- ✗ EURO
Try it
0 matches (JavaScript engine)
USD
EUR
JPY
AED
usd
XYZ
US$
EUROCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$/g;
const text = "USD";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$')
text = 'USD'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$/';
$text = 'USD';
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("^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$");
String text = "USD";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$`)
text := "USD"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$");
var text = "USD";
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"^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$").unwrap();
let text = "USD";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$/
text = 'USD'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why not just accept any three uppercase letters?
- A plain [A-Z]{3} accepts thousands of combinations that are not currencies, so typos slip through. Listing known codes gives a much stronger check for the cost of a slightly longer pattern.
- How do I add another currency?
- Insert the code into the alternation inside the group, separated by a pipe character, for example |THB before the closing parenthesis. Keep codes in uppercase to match the ISO standard.
- Does ISO 4217 also define numeric codes?
- Yes. Each currency has a three digit numeric code as well, such as 840 for the US dollar. This pattern handles only the alphabetic codes, which are far more common in user-facing data.
Related patterns
- Euro amount^(?:€ ?(?:\d{1,3}(?:[.,]\d{3})*|\d+)(?:[.,]\d{2})?|(?:\d{1,3}(?:[. ]\d{3})*|\d+)(?:,\d{2})? ?€)$
- US dollar amount^-?\$(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$
- 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}$