1. Regular Expressions
1.1. Remove leading whitespace
Match: \A\s+
Replace: none
1.2. Remove trailing whitespace
Match: \s+\Z
Replace: none
2. Code
import re
subject = " nihao "
subject = re.sub("\A\s+", "", subject)
result = re.sub("\s+\Z", "", subject)
print result
3. string.strip
import string
subject = " nihao "
result = string.strip(subject)
print result
Python 正则表达式去除前导和后缀空格
最新推荐文章于 2023-12-20 21:00:00 发布