我想处理这些字符串,例如:
"I will meet you at 1st."
"5th... OK, 5th?"
"today is 2nd\n"
"Aug.3rd"
用其他对应的字符串替换“st|nd|rd|th”,实际上是xml标签,我想将那些“1st, 2nd, 3rd, 4th”变成上标的样子:
1<Font Script=”super”>rd</Font>
5<Font Script=”super”>th</Font> ... OK, 5<Font Script=”super”>th</Font>?
像这样
最佳答案
使用 re 模块来识别日期模式并替换它们。
>>> re.sub(r"([0123]?[0-9])(st|th|nd|rd)",r"\1<sup>\2</sup>","Meet you on 5th")
'Meet you on 5<sup>th</sup>'
正则表达式演示:http://regexr.com/38lao
关于Python处理日期格式如 "1st, 2nd, 3rd, 4th",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22805707/