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 problem is matching only valid roman numerals. Eg, 990 is NOT "XM", it's "CMXC"
My problem in making the regex for this is that in order to allow or not allow certain characters, I need to look back. Let's take thousands and hundreds, for example.
I can allow M{0,2}C?M (to allow for 900, 1000, 1900, 2000, 2900 and 3000). However, If the match is on CM, I can't allow following characters to be C or D (because I'm already at 900).
How can I express this in a regex?
If it's simply not expressible in a regex, is it expressible in a context-free grammar?
Try:
Breaking it down:
This specifies the thousands section and basically restrains it to between
0
and4000
. It's a relatively simple:Slightly more complex, this is for the hundreds section and covers all the possibilities:
Same rules as previous section but for the tens place:
This is the units section, handling
0
through9
and also similar to the previous two sections (Roman numerals, despite their seeming weirdness, follow some logical rules once you figure out what they are):Actually, your premise is flawed. 990 IS "XM", as well as "CMXC".
The Romans were far less concerned about the "rules" than your third grade teacher. As long as it added up, it was OK. Hence "IIII" was just as good as "IV" for 4. And "IIM" was completely cool for 998.
(If you have trouble dealing with that... Remember English spellings were not formalized until the 1700s. Until then, as long as the reader could figure it out, it was good enough).
As Jeremy and Pax pointed out above ... '^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$' should be the solution you're after ...
The specific URL that should have been attached (IMHO) is http://thehazeltree.org/diveintopython/7.html
Example 7.8 is the short form using {n,m}
In my case, I was trying to find and replace all occurences of roman numbers by one word inside the text, so I couldn't use the start and end of lines. So the @paxdiablo solution found many zero-length matches. I ended up with the following expression:
My final Python code was like this:
Output:
Just to save it here:
Matches all the Roman numerals. Doesn't care about empty strings (requires at least one Roman numeral letter). Should work in PCRE, Perl, Python and Ruby.
Online Ruby demo: http://rubular.com/r/KLPR1zq3Hj
Online Conversion: http://www.onlineconversion.com/roman_numerals_advanced.htm
To avoid matching the empty string you'll need to repeat the pattern four times and replace each
0
with a1
in turn, and account forV
,L
andD
:In this case (because this pattern uses
^
and$
) you would be better off checking for empty lines first and don't bother matching them. If you are using word boundaries then you don't have a problem because there's no such thing as an empty word. (At least regex doesn't define one; don't start philosophising, I'm being pragmatic here!)In my own particular (real world) case I needed match numerals at word endings and I found no other way around it. I needed to scrub off the footnote numbers from my plain text document, where text such as "the Red Seacl and the Great Barrier Reefcli" had been converted to
the Red Seacl and the Great Barrier Reefcli
. But I still had problems with valid words likeTahiti
andfantastic
are scrubbed intoTahit
andfantasti
.