Pattern library / Validation
Country Code Regex (ISO 3166)
Two letters, uppercase: that is the alpha-2 shape every API expects (ID, US, GB). Regex can prove the shape — two letters, no more — but not that the code is one of the ~249 real countries; that step is a set lookup, and the notes show the exact pattern for each ISO variant.
How the pattern works
^[A-Z]{2}$— alpha-2: two uppercase letters, anchored. Rejectsusa(three) andus1(digit).- Alpha-3:
^[A-Z]{3}$. Numeric (ISO 3166-1 numeric, what UN and World Bank data uses):^[0-9]{3}$. - Shape is not membership:
XXandZZpass perfectly and do not exist. Membership check:new Set(['ID','US',…]).has(code)in JavaScript,IN UNNEST([…])against a country dimension table in BigQuery. - GA4's country dimension stores full names (
Indonesia), not codes — convert with a lookup table before comparing to alpha-2 values.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
ID | Match | Indonesia, alpha-2 | Try → |
US | Match | United States, alpha-2 | Try → |
GB | Match | United Kingdom, alpha-2 | Try → |
us | No match | lowercase — uppercase first | Try → |
USA | No match | alpha-3 — needs the {3} variant | Try → |
US1 | No match | digits do not belong in alpha codes | Try → |
Where to use it
JavaScript (shape + membership)
const isShape = /^[A-Z]{2}$/.test('ID'); // true: looks like a code
const COUNTRIES = new Set(['ID', 'US', 'GB', 'DE']); // real members
const real = COUNTRIES.has('ID'); // true
Python
import re
SHAPE = r'^[A-Z]{2}$'
assert SHAPE.fullmatch('ID')
# membership: pycountry, or your own dict of ~249 ISO 3166-1 codes
BigQuery (RE2)
SELECT country_code,
REGEXP_CONTAINS(country_code, r'^[A-Z]{2}$') AS shape_ok
FROM project.dataset.countries
Common mistakes
XX,QQ,ZZ— well-formed, nonexistent. Always pair shape with membership if the value drives logic downstream.- Lowercase codes arrive from APIs all the time (
us) — uppercase before testing; do not flip the regex to case-insensitive if sloppy input should be reported. - Mixing alpha-2 and alpha-3 is the #1 integration bug:
USAfailing your {2} test means the data is alpha-3, not that the regex is wrong.
FAQ
How do I check a real ISO 3166 country code?
Two steps: shape (^[A-Z]{2}$) then membership — a Set or array of the 249 assigned codes, or a package like pycountry. Regex alone cannot know that QQ is unassigned.
What is the difference between alpha-2, alpha-3 and numeric codes?
alpha-2 = 2 letters (ID), alpha-3 = 3 letters (IDN), numeric = 3 digits (360). Three shapes, three tiny regexes — pick the one your data source actually emits.
Does GA4 store country codes?
GA4 stores full country names in its country dimension. Convert names to alpha-2 with a lookup table before pattern-matching; keep the join in SQL, not in the regex.
Related patterns
IBAN Regex — Validate International Bank Account Numbers · Phone Number Regex — International & US Formats · UTM Parameter Regex — Match & Extract utm_* Tags
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.