我刚开始玩弄正则表达式。我查看了Google 的 Python regex howto和Python 的 regex howto以及其他类似的问题,例如将包含罗马数字的字符串转换为等效整数以及如何仅将有效的罗马数字与正则表达式匹配?,但我仍然很困惑。
我的代码:
user = str(input("Input the Roman numeral: "))
characters = "I", "V" "X", "L", "C", "D", "M"
values = 1, 5, 10, 50, 100, 500, 1000
def numerals(match):
return str(user(match.group(0)))
s = str(input("Input the Roman numeral: "))
regex = re.compile(r'\b(?=[MDCLXVI]+\b)M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V? I{0,3})\b')
print regex.sub(numerals, s)
最后两行来自第一个链接。我不完全理解regex = re.compiler...
,想知道它是否真的将用户的罗马数字转换为整数?提前致谢