在Python中围绕匹配添加标记

时间:2016-03-28 16:53:44

标签: python regex

我目前正在尝试编写一段代码,在文本中找到的数字周围添加html粗体标记。

这是我的输入文字:

我认为他是100%的人。我认为他是100%的人。我认为他是百分之百的人

这就是我想要的:

我认为他是 100%的家伙。我认为他是一个 100%的家伙。我认为他是 100%的人

但这就是我得到的:

我认为他是<100% 100%的人。我认为他是一个 100% 100%的家伙。我认为他是一个<100% 100%的人。

import re

taggedOutput=""
myInput = "I think he was a 100 percent guy \n I think he was a 100 % \guy \n I think he was a 100 per cent guy"
pattern ="(([0-9]+ ?)(%|percent|per cent))"
regex = re.compile(pattern)

# I use re.split to isolate the content I want to tag... I guess the problem is there...
chunks = re.split(regex,myInput)
#For every chunk
for chunk in chunks:
    if chunk != None:
        matchStat = re.match(regex,chunk)
        if matchStat:
            taggedOutput += '<B>'+chunk+'</B>'
        else :
            taggedOutput += chunk
#print
print('<P>'+taggedOutput+'</P>')

提前感谢您;)

1 个答案:

答案 0 :(得分:5)

使用re.sub()代替通过\1

引用捕获的群组
pattern = r"(([0-9]+ ?)(%|percent|per cent))"
print(re.sub(pattern, r"<b>\1</b>", myInput))
相关问题