Download Cheat sheet PDF 12 pages · syntax, editors, patterns, Unicode, performance, debugging
Pattern

Regex for Roman numerals

Standard Roman numerals (I, IV, IX, X, etc.).

The Roman numerals regex is ^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$ — copy it below, or open it in the explainer for a token-by-token breakdown.

The pattern

^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$
Try in explainer → Download cheat sheet ↓

What it matches

  • IX
  • XLII
  • MMXXIV
  • MCMLXXXIV
  • I

What it doesn't match

  • IIII
  • VV
  • IC
  • random

Notes & gotchas

Strict Roman numerals 1-3999. Won't match invalid sequences like IIII, VV, or modern overbar notation. Empty string also "matches" (zero) — anchor with a min length if needed.

Code in your language

Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.

Open in explainer →

Token-by-token breakdown

Every part of the pattern, left to right:

TokenMeaning
^start of string (or line in multiline mode)
M{0,3}between 0 and 3 times: the literal “M”
(start of a capturing group
CMliteral text “CM”
|OR — try the alternative
CDliteral text “CD”
|OR — try the alternative
D?optional (zero or one): the literal “D”
C{0,3}between 0 and 3 times: the literal “C”
)end of group
(start of a capturing group
XCliteral text “XC”
|OR — try the alternative
XLliteral text “XL”
|OR — try the alternative
L?optional (zero or one): the literal “L”
X{0,3}between 0 and 3 times: the literal “X”
)end of group
(start of a capturing group
IXliteral text “IX”
|OR — try the alternative
IVliteral text “IV”
|OR — try the alternative
V?optional (zero or one): the literal “V”
I{0,3}between 0 and 3 times: the literal “I”
)end of group
$end of string (or line in multiline mode)

About this pattern

Number patterns appear constantly in form validation, log parsing, and data extraction. Regex is great for matching the shape but limited for semantic checks (range validation, mathematical constraints).

Quick usage in different languages

This exact pattern — with the correct escaping and idioms for each language:

  • JavaScript: /^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/.test(value)
  • Python: re.match(r"^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$", value)
  • Java: Pattern.compile("^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$").matcher(value).matches()
  • C# / .NET: Regex.IsMatch(value, @"^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$")
  • Go: regexp.MustCompile(`^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$`).MatchString(value)
  • Ruby: /^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/.match?(value)
  • PHP: preg_match('~^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$~', $value)

The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.

Common pitfalls

  • Anchored to the whole string. This pattern uses ^ and $, so it requires the entire input to match. To find it inside a longer text, drop the anchors and use the global (g) flag.
  • Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the roman numerals against a source of truth (database, API, or checksum) where it matters.

Related patterns

More patterns in the Numbers & text category:

See also

Browse all 300 patterns in the library, or open this regex in the interactive explainer to see a token-by-token breakdown, test against custom input, and generate code in seven languages.


← Back to all patterns