【发布时间】:2018-07-20 02:45:01
【问题描述】:
我在使用 re.sub 时遇到了问题。我从其他答案中了解到,这是因为我引用了一个我没有的捕获组。
我的问题是:如何调整我的代码以获得有效的组?
s = "hello a world today b is sunny c day"
markers = "a b c".split()
pattern = r'\b' + ' (?:\w+ )?(?:\w+ )?'.join(markers) + r'\b'
text = re.sub(pattern, r'<b>\1</b>', s) # this gives error
我想要这个:"hello <b>a world today b is sunny c</b> day"
【问题讨论】: