>>> import re
>>> s = 'this is an example to _remove) brackets under certain) conditions.'
>>> re.sub('(\w+)\)', lambda m: m.group(0) if m.group(0).startswith('_') else m.group(1), s)
'this is an example to _remove) brackets under certain conditions.'
re.sub
接受callable作为第二个参数,这在这里很方便:如果列表理解可以做到这一点,我不会在这里使用regex。你知道吗
如果您有以下可能的输入:
你想变成:
那你就得做:
相关问题 更多 >
编程相关推荐