re.sub('^error:', '', 'ERROR: device not found', flags=re.IGNORECASE)
# '^error:' means the start of the string is error:
# '' means replace it with nothing
# 'ERROR: device not found' is your error string
# flags=re.IGNORECASE means this is a case insensitive search.
# In your case it would probably be this:
re.sub('^error:', '', error, flags=re.IGNORECASE)
可以使用正则表达式:
结果:
^{pr2}$说明
pattern
是一个正则表达式,这里我们使用一个前缀为r
的标准字符串来指定它是一个正则表达式。在replacement
是您想要用来代替匹配的内容,一个要删除的空字符串。在inputString
是您要搜索的字符串,您的错误消息。在count
要替换的发生次数,仅在开始时替换一个。在flags
:re.I
或{re
模块可能是您的最佳选择:这将删除字符串开头的
ERROR:
的所有变体。在您有以下变量:
现在,如果您想从中删除错误,只需删除字符串的前6个条目(如果您还想删除:)。像
^{pr2}$它给出了:
这个命令只将条目6带到:字符串的末尾。 希望这对你有帮助。当然,只有当错误出现在字符串的开头时,这才有效
相关问题 更多 >
编程相关推荐