I've tried this for the italics:
r = re.compile(r"(\*[^ ]+\*)")
r.sub(r'<i>"\1"</i>', foo)
but it doesn't work, as I am sure anyone in the regex know will see right away.
I've tried this for the italics:
r = re.compile(r"(\*[^ ]+\*)")
r.sub(r'<i>"\1"</i>', foo)
but it doesn't work, as I am sure anyone in the regex know will see right away.
It would easily work, if you switch the order of substitutions. Handling the bold case first would prevent italics taking over.
Your regex and substitution need a few tweaks.
r = re.compile(r"(\*[^ ]+\*)")
You are capturing a bit too much here -- the asterisks are preserved in \1
.
r.sub(r'<i>"\1"</i>', foo)
You are substituting a bit too much here -- the double-quote marks are included in the substitution. Example:
r.sub(r'<i>"\1"</i>', '*foo*') # -> '<i>"*foo*"</i>'
Try something like this:
foo = '***foo***'
bold = re.compile(r'''\*\*([^ ]+)\*\*''')
ital = re.compile(r'''\*([^ ]+)\*''')
ital.sub(r'''<i>\1</i>''', bold.sub(r'''<b>\1</b>''', foo)) # '<b><i>foo</i></b>'