import re

class Solution:
    def romanToInt(self, s: str) -> int:
        roman_numerals = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
        result = 0
        for i, c in enumerate(s):
            if (i + 1) == len(s) or roman_numerals[c] >= roman_numerals[s[i + 1]]:
                result += roman_numerals[c]
            else:
                result -= roman_numerals[c]
        if result == 0:
        	return ''
        return result

regex = r'\b(M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3}))\b'

text = """Римские числа: I, II, III, IV, V, VI, VII, VIII, IX, X, 
XII, XX, XXX, XL, L, XC, C, CD, D, CM, M, и даже MMMCMXCIX, и MMMM (не должно преобразоваться)."""

def replace_roman_with_arabic(text):
    solution = Solution()
    
    def replacer(match):
        roman_numeral = match.group(0)
        arabic_value = solution.romanToInt(roman_numeral)
        return str(arabic_value)

    return re.sub(regex, replacer, text)


if __name__ == "__main__":
	new_text = replace_roman_with_arabic(text)
	print(new_text)
