Return a copy of the string with leading and trailing characters
removed. If chars is omitted or None, whitespace characters are
removed. If given and not None, chars must be a string; the characters
in the string will be stripped from the both ends of the string this
method is called on.
import re
s = " Samsung Floor Standing Invertor Air Conditioner - 2.0 Ton - AF-28FSSDAWKXFA (Q-9000) - White (Brand Warranty) "
s = re.sub("^\s+|\s+$","",s)
print(s)
结果:
"Samsung Floor Standing Invertor Air Conditioner - 2.0 Ton -
AF-28FSSDAWKXFA (Q-9000) - White (Brand Warranty)"
这不需要使用正则表达式。你知道吗
.strip()就是你需要的。你知道吗
实时PYTHON演示
http://ideone.com/iEmH0i
如果你必须使用regex,你可以用这个:
结果:
只是为了好玩:一个使用regex的解决方案(因为这样更好
strip
)你得到了
相关问题 更多 >
编程相关推荐