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
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| M | Matches the literal character 'M' |
| {0,3} | Matches between 0 and 3 times |
| ( | Start of capturing group |
| C | Matches the literal character 'C' |
| M | Matches the literal character 'M' |
| | | Alternation — matches the expression before OR after the pipe |
| C | Matches the literal character 'C' |
| D | Matches the literal character 'D' |
| | | Alternation — matches the expression before OR after the pipe |
| D | Matches the literal character 'D' |
| ? | Makes the preceding element optional (zero or one times) |
| C | Matches the literal character 'C' |
| {0,3} | Matches between 0 and 3 times |
| ) | End of group |
| ( | Start of capturing group |
| X | Matches the literal character 'X' |
| C | Matches the literal character 'C' |
| | | Alternation — matches the expression before OR after the pipe |
| X | Matches the literal character 'X' |
| L | Matches the literal character 'L' |
| | | Alternation — matches the expression before OR after the pipe |
| L | Matches the literal character 'L' |
| ? | Makes the preceding element optional (zero or one times) |
| X | Matches the literal character 'X' |
| {0,3} | Matches between 0 and 3 times |
| ) | End of group |
| ( | Start of capturing group |
| I | Matches the literal character 'I' |
| X | Matches the literal character 'X' |
| | | Alternation — matches the expression before OR after the pipe |
| I | Matches the literal character 'I' |
| V | Matches the literal character 'V' |
| | | Alternation — matches the expression before OR after the pipe |
| V | Matches the literal character 'V' |
| ? | Makes the preceding element optional (zero or one times) |
| I | Matches 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
| Input | Expected |
|---|---|
| XIV | Match |
| MCMXCIX | Match |
| IIII | No Match |
| IX | Match |
| ABC | No Match |
| MMXXIII | Match |
Try It — Interactive Tester
56 charsFlags: iMatches: 0Ctrl+Shift+C to copy regex