需求:
将I like <b>bold</b> and <i>italic</i> fonts变为'' I like ', '<b>', 'bold', '</b>', ' and ', '<i>', 'italic', '</i>', ' fonts'
Python:
import re
subject = 'I like <b>bold</b> and <i>italic</i> fonts'
reobj = re.compile("(<[^<>]*>)")
result = reobj.split(subject)
print result
引申:
如果只想用3个分隔符分割,可以使用
result = reobj.split(subject, 3 )
得到结果为:
' I like ', '<b>', 'bold', '</b>', ' and ', '<i>', 'italic</i> fonts'