扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

在正则表达式中匹配空格

扬州沐宇科技
2023-09-17 06:12:28
正则表达式

在正则表达式中,可以使用\s来匹配空格字符,包括空格、制表符、换行符等。如果只想匹配空格,可以使用空格字符直接匹配。以下是两个示例:

  1. 匹配任意空格字符:
import re
text = "This is a test string."
matches = re.findall(r'\s', text)
print(matches)

输出:

[' ', ' ', ' ', ' ']
  1. 匹配只有空格的字符串:
import re
text = "   "
matches = re.findall(r' ', text)
print(matches)

输出:

[' ', ' ', ' ']

扫码添加客服微信