Posts Tagged ‘regular expressions’

Regular expressions in Python

January 4, 2014

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:

WENT TO NOTEBOOK!

May be I’ll update here later.