Regular expressions in Python

Roman characters:
I = 1, V = 5, X = 10, L= 50, C= 100, D = 500, M = 1000

Caveat: There is no way to programmatically determine that two regular expressions are same. Only way to find out is: write a lot of test cases.

Regex pattern to check for roman numerals on thousands, hundreds, tens and ones places:

pattern  = ‘^M?M?M?(CD|CM|D?C?C?C?)(XL|XC|L?X?X?X?)(IV|IX|V?I?I?I?)$’

or pattern  = ‘^M{0,3}(CD|CM|D?C{0,3})(XL|XC|L?X{0,3})(IV|IX|V?I{0,3})$’

POINT: Do not put spaces in these braces.

Writing verbose regular expressions:

One’s above this are called compact regular expressions. These one’s are, as the name suggests: verbose.

Biggest difference (at least for me) between these two kind of expressions is that white-spaces are ignored in the verbose expressions while they are not in compact regular expressions. If you want to match these in a regular expression, you need to escape them by putting a backslash(\) in front of them.

Lets get started:


May be I’ll update here later.



Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: