以下方法需要简化,以使行变得相对较小。
def rep(m):
if m.group(0) == " " or m.group(0) == "_":
return "[ _]"
elif m.group(0) == "(" or m.group(0) == ")" or m.group(0) == "*" or m.group(0) == "+" or m.group(0) == "=" or m.group(0) == "?" or m.group(0) == "!" or m.group(0) == "^" or m.group(0) == "-":
return "\\" + m.group(0)
return re.sub(r"[ _()*+=?!^-]", rep, s)
答案 0 :(得分:0)
希望可以帮到您!
def rep(m):
if m.group(0) in (" ","_"):
return "[ _]"
elif m.group(0) in ("(",")","*","+","=","?","!","^","-"):
return "\\" + m.group(0)
return re.sub(r"[ _()*+=?!^-]", rep, s)