Pattern library / Validation
Roman Numeral Regex
Checking that a string is a Roman numeral — roman numeral regex, regex validate roman numerals — usually gets answered online with ^[IVXLCDM]+$, which validates the alphabet but not the numeral: it happily accepts IIII, VX and IC, none of which are legal. The pattern below validates the notation itself: ^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$ — four groups in fixed order, one per place value (thousands, hundreds, tens, ones), each offering the subtractive pair (CM = 900) before the additive form (D?C{0,3} = 100–800). Because every group only consumes its own position, out-of-order writes have no parse and fail; because every group is optional, the empty string passes too — the pitfalls section has the one-line guard. Literals, classes, groups and quantifiers only: no lookahead, no backreference, so it runs unchanged in JavaScript, Python and BigQuery (RE2).
^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$
Load this pattern into the tester →
How the pattern works
- Four place-value groups in fixed order:
M{0,3}(thousands),(CM|CD|D?C{0,3})(hundreds: subtractive CM/CD first, then additiveD?C{0,3}),(XC|XL|L?X{0,3})(tens),(IX|IV|V?I{0,3})(ones). Because each group only consumes its own digits, out-of-order strings —IC,VX,VXL,CCCC,LL,ID— have no parse and fail (all verified in the table). - Each additive group folds in its five:
D?C{0,3}covers 100–800 (D? adds 500, C{0,3} up to 300) while CM/CD handle 900/400, so every value 100–900 is reachable exactly once — the same trick for tens (L?X{0,3}+ XC/XL) and ones (V?I{0,3}+ IX/IV). That is also why the pattern stops at 3999:M{0,3}is the thousands cap. - Canonical form is uppercase:
MCMXCIVmatches,mcmxcivandxivdo not. Add theiflag in JavaScript (new RegExp(p, 'i')) or the inline(?i)in Python and other RE2-based engines; in GA4 every filter is case-sensitive by default — see the match types guide. - Anchors do the heavy lifting:
^…$forces the whole string to be one numeral. Drop them and the same pattern matches two-character substrings inside a sentence — and because every group is optional, an unanchored find also returns empty strings at every position (verified:'I took IV vitamins'yields['I','','',…,'IV',…]). Use the candidate-then-validate recipe in pitfall 3 instead.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
MCMXCIV | Match | 1994: M (1000) + CM (900) + XC (90) + IV (4) — one group per place value | Try → |
MMMCMXCIX | Match | 3999, the largest value M{0,3} allows: MMM + CM + XC + IX | Try → |
MMCDXLIV | Match | 2444: MM (2000) + CD (400) + XL (40) + IV (4) | Try → |
XIX | Match | 19: the tens group takes the plain X, the ones group takes the subtractive IX | Try → |
XL | Match | 40 — subtractive pair in the tens group; the thousands and hundreds groups match empty | Try → |
IV | Match | 4, the smallest subtractive pair — the ones group alone | Try → |
IIII | No match | non-canonical: 4 is IV, so three I's is the additive maximum — clock faces that print IIII fail here | Try → |
IC | No match | 99 must be XCIX: subtractive notation only allows IX, IV, XC, XL, CM and CD — I before C is never legal | Try → |
VX | No match | 5 then 10 is not a pair (9 is IX): the tens group comes before the ones group and cannot start with V | Try → |
MMMM | No match | 4000 is out of range: M{0,3} caps the thousands group at 3999 | Try → |
xiv | No match | lowercase fails — matching is case-sensitive by default (pitfall 2) | Try → |
MIX | Match | 1009 (M + IX) is a valid numeral — and also an English word, which is exactly why find-in-text needs the candidate filter (pitfall 3) | Try → |
Where to use it
JavaScript (validate a form value, then find numerals in text)
const ROMAN = /^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/;
const isRoman = s => s.length > 0 && ROMAN.test(s); // '' would pass the bare pattern
isRoman('MCMXCIV'); // true
isRoman('IC'); // false — 99 must be XCIX
isRoman(''); // false — the length guard (pitfall 1)
// find numerals in text: take candidates, then keep the valid ones
const hits = 'Chapter IV of MIX'.match(/\b[IVXLCDM]+\b/g) || [];
hits.filter(isRoman); // ['IV', 'MIX'] — MIX is a real numeral (1009), see pitfall 3
Python (fullmatch + extract candidates)
import re
PAT = r'^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
re.fullmatch(PAT, 'MCMXCIV') # re.Match — 1994
re.fullmatch(PAT, 'IC') # None — 99 must be XCIX
re.fullmatch(PAT, 'xiv') # None — case-sensitive (add re.I if wanted)
# pull candidates out of a text, keep the valid ones
cands = re.findall(r'\b[IVXLCDM]+\b', 'Chapter IV of MIX') # ['IV', 'MIX']
[c for c in cands if re.fullmatch(PAT, c)] # ['IV', 'MIX']
BigQuery SQL (RE2 — flag outline rows that hold a numeral)
SELECT chapter,
REGEXP_CONTAINS(chapter,
r'(?i)^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$') AS is_roman
FROM `project.dataset.outline`
-- 'MCMXCIX' → true | 'IC' → false | 'mcmxcix' → true (the (?i) flag)
Common mistakes
- The empty string passes. Every group is optional, so
re.test('')istrue— an empty input validates as a numeral. Guard it wherever it matters:s.length > 0 && re.test(s)in JavaScript, a truthiness check in Python, or the HTMLrequiredattribute next topattern. This is also why an unanchored find returns empty matches at every position. - Case-sensitivity:
xivandmcmxcivfail. Add theiflag (JavaScript) or inline(?i)(Python, RE2/BigQuery — verified above), or map input to uppercase first. GA4 filters are case-sensitive with no flag, so cover variants with[Xx]-style classes there — see GA4 regex match types. - Find-in-text false positives are inherent:
MIX(1009),CD(400),MI(1001),DI(501) and the loneI(1) are all valid numerals that collide with English words and acronyms. The recipe — extract candidates with\b[IVXLCDM]+\b, then validate each with the anchored pattern — kills non-numeral letter runs (a loneCinside “Chapter”) but cannot know context: for prose, add a surrounding requirement (e.g. after “chapter”/“vol”) or accept the ambiguity.
FAQ
How do I validate a Roman numeral with regex?
Use ^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$ — one group per place value (thousands, hundreds, tens, ones), each offering the subtractive pair before the additive form, so only canonical notation parses. Wrap it in ^…$ so the whole string must be the numeral, and add a length check because the empty string matches every group being optional. In JavaScript: s.length > 0 && re.test(s).
Why does my Roman numeral regex accept IIII, VX or other nonsense?
Because it only checks letters, not order or count: ^[IVXLCDM]+$ treats IIII, VX, IC and CCCC as fine. The place-value groups reject them — each group only consumes its own position, so an out-of-order sequence has no parse. The one thing the full pattern still accepts is the empty string (all groups optional); guard it with a length check or HTML required.
Does this pattern work in GA4, BigQuery and Python?
Yes — it uses only literals, groups, alternation and quantifiers: no lookahead, no backreference, so nothing RE2 refuses (see RE2 vs PCRE). Python: re.fullmatch(PAT, s). BigQuery: REGEXP_CONTAINS(x, r'(?i)…$') — RE2 accepts the inline (?i) flag. GA4: every filter is case-sensitive and full-match, so write the pattern exactly as shown and cover lowercase with [Xx] classes — more in the match types guide.
Related patterns
Extract Numbers from String — Regex + Examples · Alphanumeric Regex — Letters & Numbers Only · Regex Number in Range 1-100 — Match Values Between Limits
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.