在正则表达式中,可以使用\s
来匹配空格字符,包括空格、制表符、换行符等。如果只想匹配空格,可以使用空格字符直接匹配。以下是两个示例:
import re
text = "This is a test string."
matches = re.findall(r'\s', text)
print(matches)
输出:
[' ', ' ', ' ', ' ']
import re
text = " "
matches = re.findall(r' ', text)
print(matches)
输出:
[' ', ' ', ' ']
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:正则表达式空格如何匹配