2012-10-26 58 views
0

我對形式的字符串:的Python /正則表達式令牌轉換與參數採集

"Hello, this is a test. Let's tag @[William Maness], and then tag @[Another name], along with @[More Name]."

我想將其轉換成...

"Hello, this is a test. Let's tag <a href='/search/william-maness'>William Maness</a>, and then tag <a href='/search/another-name'>Another name</a>, along with [...]"

我m相當肯定這可以用正則表達式來完成,但是對我來說這太複雜了。任何幫助表示讚賞。

回答

2

re.sub()接受功能爲好,這樣你就可以處理替換文本:

import re 

text = "Hello, this is a test. Let's tag @[William Maness], and then tag @[Another name], along with @[More Name]." 

def replace(match): 
    text = match.group(1) # Extract the first capturing group 

    return '<a href="/search/{0}">{1}</a>'.format( # Format it into a link 
     text.lower().replace(' ', '-'), 
     text 
    ) 

re.sub(r'@\[(.*?)\]', replace, text) 

或者,如果你正在尋找一個可讀的一行:

>>> import re 
>>> re.sub(r'@\[(.*?)\]', (lambda m: (lambda x: '<a href="/search/{0}">{1}</a>'.format(x.lower().replace(' ', '-'), x))(m.group(1))), text) 
'Hello, this is a test. Let\'s tag <a href="/search/william-maness">William Maness</a>, and then tag <a href="/search/another-name">Another name</a>, along with <a href="/search/more-name">More Name</a>.' 
+0

這是史詩!謝謝攪拌機。這完全是我需要的。耗時4分鐘。我在你的正則表達式中向你鞠躬致敬。 –

2

您可以匹配任何這樣的名字:

r'@\[([^]]+)\]' 

捕獲組包圍在原文中括號內的名稱。

然後,您可以使用傳遞給sub()功能替代的鏈接名稱的基礎上,查找您有:

def replaceReference(match): 
    name = match.group(1) 
    return '<a href="/search/%s">%s</a>' % (name.lower().replace(' ', '-'), name) 

refs = re.compile(r'@\[([^]]+)\]') 
refs.sub(replaceReference, example) 

功能是通過每個找到匹配的Match對象;捕獲組使用.groups(1)進行檢索。

在這個例子中,名稱以非常簡單的方式進行轉換,但是例如,如果名稱存在,則可以執行實際的數據庫檢查。

演示:

>>> refs.sub(replaceReference, example) 
'Hello, this is a test. Let\'s tag <a href="/search/william-maness">William Maness</a>, and then tag <a href="/search/another-name">Another name</a>, along with <a href="/search/more-name">More Name</a>.' 
0

使用@ Martijn的正則表達式:

>>> s 
"Hello, this is a test. Let's tag @[William Maness], and then tag @[Another name], along with @[More Name]." 
>>> re.sub(r'@\[([^]]+)\]', r'<a href="/search/\1</a>', s) 
'Hello, this is a test. Let\'s tag <a href="/search/William Maness</a>, and then tag <a href="/search/Another name</a>, along with <a href="/search/More Name</a>.' 

儘管如此,您仍然需要扼殺您的用戶名。

相關問題