Question

I want to deal with those strings like:

"I will meet you at 1st."
"5th... OK, 5th?"
"today is 2nd\n"
"Aug.3rd"

To replace the "st|nd|rd|th" with other corresponsive string, actually are xml tags, I want to make those "1st, 2nd, 3rd, 4th" into superscript looks:

1<Font Script=”super”>rd</Font>
5<Font Script=”super”>th</Font> ... OK, 5<Font Script=”super”>th</Font>?

Like this

Was it helpful?

Solution

Use re module to identify the date patterns and replace them.

>>> 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>'

Regex demo: http://regexr.com/38lao

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top