在python3中,如何在任何字符之后或之前立即从字符串中删除+和-符号



来自以下输入字符串

INPUT => --- text1 +++ text2 @@ -71,0 +72,4 @@ +t+o+o+l
OUTPUT => --- text1 +++ text2 @@ -71,0 +72,4 @@ tool

如何从字符串中删除+和-符号?

此代码将在字母(小写或大写(之前或之后立即替换+或-。这可能只需要一个正则表达式,但我无法理解!

import re
line = re.sub(r"[+-]([a-zA-Z])", "g<1>", string)
line = re.sub(r"([a-zA-Z])[+-]", "g<1>", line)

您可以这样做:

import re
s = input()
s = re.sub(r"[+-]([a-zA-Z])", "g<1>", s)
s = re.sub(r"([a-zA-Z])[+-]", "g<1>", s)
print(s)

这将删除加号和减号。

最新更新