【发布时间】:2010-01-19 16:20:39
【问题描述】:
大家好,
我不确定这是否可行,但我想在正则表达式替换中使用匹配组来调用变量。
a = 'foo'
b = 'bar'
text = 'find a replacement for me [[:a:]] and [[:b:]]'
desired_output = 'find a replacement for me foo and bar'
re.sub('\[\[:(.+):\]\]',group(1),text) #is not valid
re.sub('\[\[:(.+):\]\]','\1',text) #replaces the value with 'a' or 'b', not var value
想法?
【问题讨论】:
-
哈!并不真地。熟悉 py、perl 和 php - 无一精通。谢谢你的帮助:)