python 如何给一段文本中的某些字符添加数字编号?

2025-03-31 17:24:09
推荐回答(1个)
回答1:

#-*coding:utf-8-*
import re
def createid(matchobject,no=[0]):
    no[0]+=1
    return "[%d]"%no[0]
text = "★A child is a human being who is not yet an adult.★A child is a human being who is not yet an adult.★A child is a human being who is not yet an adult."
text=re.sub("★",createid,text)
print(text)