Thinking about my other problem, i decided I can\'t even create a regular expression that will match roman numerals (let alone a context-free grammar that will generate them
The following expression worked for me to validate the roman number.
^M{0,4}(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$
Here,
M{0,4}
will match thousandsC[MD]|D?C{0,3}
will match HundredsX[CL]|L?X{0,3}
will match TensI[XV]|V?I{0,3}
will match UnitsBelow is a visualization that helps to understand what it is doing, preceded by two online demos:
Debuggex Demo
Regex 101 Demo
Python Code:
import re
regex = re.compile("^M{0,4}(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$")
matchArray = regex.match("MMMCMXCIX")