Python 重新拆分(";\W";)=重新拆分(";\W";)?

Python 重新拆分(";\W";)=重新拆分(";\W";)?,python,python-3.x,split,Python,Python 3.x,Split,正如标题所述,re.split(“\W”)与re.split(“\W”)相同,因为我得到的结果与我使用的结果相同。如果它有或没有+,情况也是如此。是这样吗?或者它在某些情况下有效,如果是,为什么?提前谢谢。它们根本不是一回事: >>> test_string = 'hello world' >>> import re >>> re.split('\w', test_string) ['', '', '', '', '', ' ', '', '

正如标题所述,
re.split(“\W”)
re.split(“\W”)
相同,因为我得到的结果与我使用的结果相同。如果它有或没有
+
,情况也是如此。是这样吗?或者它在某些情况下有效,如果是,为什么?提前谢谢。

它们根本不是一回事:

>>> test_string = 'hello world'
>>> import re
>>> re.split('\w', test_string)
['', '', '', '', '', ' ', '', '', '', '', '']
>>> re.split('\W', test_string)
['hello', 'world']
是否有下列情况:

按模式的出现次数拆分源字符串, 返回包含结果子字符串的列表

\w
\w
是:


它们不一样,它们是对立的。显示输入字符串以使问题更清楚我建议使用
\W+
,因为它更一般。例如,如果有
“英国伦敦”
,则返回
[“伦敦”、“英国”、“英国”]
。如果没有
+
,它将返回:
['London',''unity','Kingdom']
(请注意额外的空字符串)。
\w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_].
         With LOCALE, it will match the set [0-9_] plus characters defined
         as letters for the current locale.
\W       Matches the complement of \w.