# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = 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})"
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"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE | re.IGNORECASE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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