假设有一行文字:
SUBSTR(name,1,20) first_name, last_name, name
我想打印整行,不包括SUBSTR(name,1,20)
。我想用正则表达式来表达这个,但我被卡住了,因为我不懂正则表达式。你知道吗
我只为SUBSTR写了一篇文章,但没用。你知道吗
import re
x="SUBSTR Hi"
func= re.sub("\bSUBSTR\b","",x)
f=x.strip()
print(f)
它打印整个x值,而不只是Hi。
如何删除SUBSTR(name,1,20)
?我不想像x.strip(SUBSTR(name,1,20)
那样直接使用strip,因为我还有几个模式要写。你知道吗
在
re.sub
中使用模式(\b[A-Z]+\(.*?\))
例如:
输出:
要使用可选的后跟非空白序列剥离特定图案,请执行以下操作:
正则表达式:
(\b(SUBSTR)+\(.*?,.*?,.*?\))
输出:
模式删除字符串中以SUBSTR开头的部分,以及在()中保留在其后的所有内容。如果存在(),则必须有三个delimeter值','。例如,它不会删除
SUBSTR()
,因为没有输入值。如果必须删除,则使用\b(SUBSTR)+\(.*?\))
。你知道吗相关问题 更多 >
编程相关推荐