当前正在使用瓶子的Web服务器上工作,并尝试实现接受消息并将其转换为适合显示的HTML的函数。 还有就是将多个不同的主题标签转化为与python的链接
Any hashtags in the text ('#' followed by a sequence of letters, numbers or periods)
are enclosed by a <strong class='hashtag'> tag.
Eg. <strong class='hashtag'>#whatever</strong>
我有一个问题与时出现在文本块不止一个主题标签,如使用正则表达式让我找到了一定的主题标签块,但re.sub替换的部分限制所有哈希标签都带有一个给定值。 这是我有:
def post_to_html(content):
if (re.search(r'#[\d\w\.]*', content) is not None):
hold = re.search(r'#[\d\w\.]*', content).group(0)
repltxt = "<strong class='hashtag'>{0}</strong>".format(hold)
hold = re.sub(r'#[\d\w\.]*', repltxt, content)
content = hold
给出当其中:
"#whatever you #want"
输出:
<strong class='hashtag'>#whatever</strong> you <strong class='hashtag'>#whatever</strong>
谢谢你,有没有什么机会可以解释\ g <1>和\ 1实际上对未来的参考作用,所以我明白我是如何使用它的。 – Phalaxion
嘿@Phalaxion,在反向引用上编辑了更多答案;)。另外,替换字符串上的'\ g <1>'替换为'\ 1',只是为了使它更简单一些。 –