小编net*_*ate的帖子

Python re.sub问题

问候所有,

我不确定这是否可行,但我想在正则表达式替换中使用匹配的组来调用变量.

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
Run Code Online (Sandbox Code Playgroud)

想法?

python regex

15
推荐指数
2
解决办法
1万
查看次数

在Python中启用Unicode"全局"

是否有可能避免将其放在每一页?

 # -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)

我真的很喜欢Python默认这个.提前致谢!

python unicode

3
推荐指数
1
解决办法
2926
查看次数

标签 统计

python ×2

regex ×1

unicode ×1