Advertisement

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).

The pattern
^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

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
MCMXCIVMatch1994: M (1000) + CM (900) + XC (90) + IV (4) — one group per place valueTry →
MMMCMXCIXMatch3999, the largest value M{0,3} allows: MMM + CM + XC + IXTry →
MMCDXLIVMatch2444: MM (2000) + CD (400) + XL (40) + IV (4)Try →
XIXMatch19: the tens group takes the plain X, the ones group takes the subtractive IXTry →
XLMatch40 — subtractive pair in the tens group; the thousands and hundreds groups match emptyTry →
IVMatch4, the smallest subtractive pair — the ones group aloneTry →
IIIINo matchnon-canonical: 4 is IV, so three I's is the additive maximum — clock faces that print IIII fail hereTry →
ICNo match99 must be XCIX: subtractive notation only allows IX, IV, XC, XL, CM and CD — I before C is never legalTry →
VXNo match5 then 10 is not a pair (9 is IX): the tens group comes before the ones group and cannot start with VTry →
MMMMNo match4000 is out of range: M{0,3} caps the thousands group at 3999Try →
xivNo matchlowercase fails — matching is case-sensitive by default (pitfall 2)Try →
MIXMatch1009 (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

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

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.

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.