re里的sub方法中的参数,可以用函数

def dashrepl(matchobj):
    if matchobj.group(0) == '-': return ' '
    else: return '-'
re.sub('-{1,2}', dashrepl, 'pro----gram-files')

re.sub(r'sANDs', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
原文地址:https://www.cnblogs.com/lanhoo/p/9709335.html