【发布时间】:2020-12-24 13:19:24
【问题描述】:
我目前正在研究正则表达式并且遇到了一个问题。
所以问题的标题是我想要找出的。我想既然\s代表一个空格,re.split(" ", string)和re.split("\s+", string)会给出相同的值,如下所示:
>>> import re
>>> a = re.split(" ", "Why is this wrong")
>>> a
["Why", "is", "this", "wrong"]
>>> import re
>>> a = re.split("\s+", "Why is this wrong")
>>> a
["Why", "is", "this", "wrong"]
这两个给出了相同的答案,所以我认为它们是相同的。然而,事实证明这些是不同的。在什么情况下会有所不同?我在这里错过了什么让我眼花缭乱?
【问题讨论】:
-
"\s+"代表一个或多个 any 空格,包括" ", "\t", "\n"和更多。" "只是一个空格字符。 -
@schwobaseggl 所以
\s还可以表示不止" ",还可以表示Enter (which is equal to \n),或者" ", with two space characters? -
这能回答你的问题吗? Reference - What does this regex mean?