Understanding And Using S Regular Expression In Python
Understanding And Using S Regular Expression In Python
\s+ 是一个常用的正则表达式,含义如下:
语法解析
\s匹配任意空白字符,包括:- 空格(space)
- 制表符(tab,
\t) - 换行符(
\n) - 回车符(
\r) - 换页符(
\f) - 垂直制表符(
\v)
+表示匹配前面的模式一次或多次(至少 1 次,连续匹配)。
综合含义
\s+ 匹配 一个或多个连续的空白字符。
常见用途
- 去除多余空格
import re
text = "Hello World Python"
result = re.sub(r"\s+", " ", text) # 将多个空白替换为一个空格print(result) # 输出: Hello World Python
- 按空白分割字符串
import re
text = "apple banana\torange\npear"
words = re.split(r"\s+", text)
print(words) # ['apple', 'banana', 'orange', 'pear']
- 匹配空白区域(如 HTML 中的缩进、换行等)
✅ 注意:
- 如果只想匹配空格(不包括换行、制表符等),应使用
" +"而不是\s+。 - 在不同语言的正则引擎中,
\s的定义可能略有差异,但大多数遵循 Unicode 空白字符规则。
This post is licensed under CC BY 4.0 by the author.