當前正在使用瓶子的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',只是爲了使它更簡單一些。 –