Input Format
A single line of input containing a string of Roman characters.
Output Format
Output a single line containing True or False according to the instructions above.

hackerrank validating roman numerals problem solution in python

Problem solution in Python 3 programming language.

import re
print(bool(re.search(r"^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$",input())))

Second solution

thousand = 'M{0,3}'
hundred = '(C[MD]|D?C{0,3})'
ten = '(X[CL]|L?X{0,3})'
digit = '(I[VX]|V?I{0,3})'
regex_pattern = r"%s%s%s%s$" % (thousand, hundred, ten, digit)
import re
print(str(bool(re.match(regex_pattern, input()))))

Problem solution in Python 2 programming language.

import re
print bool(re.search(r"^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$",raw_input()))