Regex to Match Roman Numerals

Validate Roman numeral strings from I to MMMCMXCIX (1-3999). Handles subtractive notation like IV, IX, XL, XC, CD, and CM correctly.

Regular Expression

/^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/i

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
MMatches the literal character 'M'
{0,3}Matches between 0 and 3 times
(Start of capturing group
CMatches the literal character 'C'
MMatches the literal character 'M'
|Alternation — matches the expression before OR after the pipe
CMatches the literal character 'C'
DMatches the literal character 'D'
|Alternation — matches the expression before OR after the pipe
DMatches the literal character 'D'
?Makes the preceding element optional (zero or one times)
CMatches the literal character 'C'
{0,3}Matches between 0 and 3 times
)End of group
(Start of capturing group
XMatches the literal character 'X'
CMatches the literal character 'C'
|Alternation — matches the expression before OR after the pipe
XMatches the literal character 'X'
LMatches the literal character 'L'
|Alternation — matches the expression before OR after the pipe
LMatches the literal character 'L'
?Makes the preceding element optional (zero or one times)
XMatches the literal character 'X'
{0,3}Matches between 0 and 3 times
)End of group
(Start of capturing group
IMatches the literal character 'I'
XMatches the literal character 'X'
|Alternation — matches the expression before OR after the pipe
IMatches the literal character 'I'
VMatches the literal character 'V'
|Alternation — matches the expression before OR after the pipe
VMatches the literal character 'V'
?Makes the preceding element optional (zero or one times)
IMatches the literal character 'I'
{0,3}Matches between 0 and 3 times
)End of group
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates Roman numerals from 1 to 3999 using proper subtractive notation. Here is the token-by-token breakdown:

^ — Anchors the match at the start of the string.

M{0,3} — Matches zero to three M characters for the thousands place. M represents 1000, so this handles values from 0 to 3000.

(CM|CD|D?C{0,3}) — Capturing group 1 handles the hundreds place. CM matches 900, CD matches 400, and D?C{0,3} handles 0-300 and 500-800. The optional D represents 500, and up to three C characters each represent 100.

(XC|XL|L?X{0,3}) — Capturing group 2 handles the tens place. XC matches 90, XL matches 40, and L?X{0,3} handles 0-30 and 50-80. The optional L represents 50, and up to three X characters each represent 10.

(IX|IV|V?I{0,3}) — Capturing group 3 handles the ones place. IX matches 9, IV matches 4, and V?I{0,3} handles 0-3 and 5-8. The optional V represents 5, and up to three I characters each represent 1.

$ — Anchors the match at the end of the string.

The i flag makes the match case-insensitive so both XIV and xiv are accepted. This pattern enforces the standard rules of Roman numeral notation: no more than three consecutive identical characters, and proper use of subtractive pairs. It is useful for input validation in forms, document numbering systems, clock faces, and educational applications dealing with Roman numerals.

Example Test Strings

InputExpected
XIVMatch
MCMXCIXMatch
IIIINo Match
IXMatch
ABCNo Match
MMXXIIIMatch

Try It — Interactive Tester

//i
gimsuy
No matches found.
Pattern: 56 charsFlags: iMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →