2010-11-21 98 views
0

我有一个字符串。 。Python:替换标签但保留内部文本?

"This is an [[example]] sentence. It is [[awesome]]

我想<b>.</b>更换的[[.]]所有实例保留通过.

匹配通配符文本的结果应该是: "This is an <b>example</b> sentence. It is <b>awesome</b>”。

我可以去和手动<b>]]</b>取代[[,但它更有意义,只是做了一次,并保留标签之间的文本。

我该怎么做?

注意:这是为了从数据库中获取源代码并将其转换为HTML。它应该模仿wiki风格的语法。在这种情况下,[[x]]会以粗体字显示。

回答

5

你可以使用弦上replace方法。

>>> s = 'This is an [[example]] sentence. It is [[awesome]].' 
>>> s.replace('[[', '<b>').replace(']]', '</b>') 

'This is an <b>example</b> sentence. It is <b>awesome</b>.' 

只是为了得到一些timeit结果在这里:

$ python -mtimeit -s'import re' "re.sub(r'\[\[(.*?)\]\]', r'<b>\1</b>', 'This is an [[example]] sentence. It is [[awesome]]')"'' 
100000 loops, best of 3: 19.7 usec per loop 

$ python -mtimeit '"This is an [[example]] sentence. It is [[awesome]]".replace("[[", "<b>").replace("]]", "</b>")' 
100000 loops, best of 3: 1.94 usec per loop 

如果我们编译的正则表达式,我们得到表现稍好:

$ python -mtimeit -s"import re; r = re.compile(r'\[\[(.*?)\]\]')" "r.sub(r'<b>\1</b>', 'This is an [[example]] sentence. It is [[awesome]]')" 
100000 loops, best of 3: 16.9 usec per loop 
2

该代码允许您随意扩展替换列表。

import re 

_replacements = { 
    '[[': '<b>', 
    ']]': '</b>', 
    '{{': '<i>', 
    '}}': '</i>', 
} 

def _do_replace(match): 
    return _replacements.get(match.group(0)) 

def replace_tags(text, _re=re.compile('|'.join(re.escape(r) for r in _replacements))): 
    return _re.sub(_do_replace, text) 

print replace_tags("This is an [[example]] sentence. It is [[{{awesome}}]].") 

This is an <b>example</b> sentence. It is <b><i>awesome</i></b>. 
3

如何使用re.sub()和一点点正则表达式魔术:

import re 
re.sub(r'\[\[(.*?)\]\]', r'<b>\1</b>', "This is an [[example]] sentence. It is [[awesome]]"); 
+3

\ 1”应该是R“\ 1‘或’\\ 1”,让反斜线被正确地传递给正则表达式引擎,而不是转换为ASCII 001一旦这样改变了它的工作原理大。 – cecilkorik 2010-11-21 03:29:23

+0

'@ aaronasterling'和'@ cecilkorik'谢谢你们,我把它遗漏了,当我看到我的错误时立即编辑了我的答案。 :) – Alex 2010-11-21 03:30:47

0

的方法由其他海报肯定会建议工作,但是我想指出的是,使用正则表达式来完成这个任务会带来相当大的性能影响。

您提供的示例也可以使用本地Python字符串操作来解决,并且执行速度将快大约3倍。

例如:

>>> import timeit 
>>> st = 's = "This is an [[example]] sentence. It is [[awesome]]"' 
>>> t = timeit.Timer('s.replace("[[","<b>").replace("]]","</b>")',st) 
>>> t.timeit() # Run 1000000 times 
1.1733845739904609 
>>> tr = timeit.Timer("re.sub(r'\[\[(.*?)\]\]', r'<b>\1</b>',s)",'import re; ' + st) 
>>> tr.timeit() # Run 1000000 times 
3.7482673050677704 
>>> 

希望这有助于:)

+0

@Reznor这是否回答你的问题? – nonot1 2010-11-22 04:35:12

1

...使用正则表达式的方法在这里可能是,它可以防止在做替换时,源文本没有的优势有匹配的配对[[]]

也许很重要,也许不是。