import re
regex = re.compile(r"(?=\b[MCDXLVI]{1,6}\b)M{0,4}(?:CM|CD|D?C{0,3})(?:XC|XL|L?X{0,3})(?:IX|IV|V?I{0,3})", flags=re.MULTILINE | re.IGNORECASE)
test_str = ("Act IX: The End\n"
"Act iX: The End\n"
"Act Ix: The End\n"
"Act ix: The End\n"
"Act IX : The End\n"
"Act iX : The End\n"
"Act Ix : The End\n"
"Act ix : The End\n"
"i also me\n"
"only ii two\n"
"the iii three\n"
"what about iv\n\n"
"I\n"
"II\n"
"III\n"
"IV\n"
"V\n"
"VI\n"
"VII\n"
"VIII\n"
"IX\n"
"X\n"
"XI\n"
"XII\n"
"XIII\n"
"XIV\n"
"XV\n"
"XVI\n"
"XVII\n"
"XVIII\n"
"XIX\n"
"XX\n"
"XLIV\n"
"XLIX\n"
"LXXIX\n"
"XCIV\n"
"XCIX\n"
"C\n"
"CI\n"
"CII\n"
"CIII\n"
"CIV\n"
"CVI\n"
"CIX\n"
"CD\n"
"CM\n"
"CC\n"
"CCC\n"
"D\n"
"DC\n"
"DCC\n"
"DCCC\n"
"CX\n"
"CXX\n"
"CXIX\n"
"CMXCIX\n")
subst = "\\U$0"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html