-3
如标题所示,re.split("\W")
与re.split("\w")
相同,因为我得到的结果与我使用的相同。如果它有+
也是如此。这是正确的吗?或者它在某些情况下有效,如果是,为什么?先谢谢你。is re.split(“ W”)= re.split(“ w”)?
如标题所示,re.split("\W")
与re.split("\w")
相同,因为我得到的结果与我使用的相同。如果它有+
也是如此。这是正确的吗?或者它在某些情况下有效,如果是,为什么?先谢谢你。is re.split(“ W”)= re.split(“ w”)?
他们不是在所有同一件事:
>>> test_string = 'hello world'
>>> import re
>>> re.split('\w', test_string)
['', '', '', '', '', ' ', '', '', '', '', '']
>>> re.split('\W', test_string)
['hello', 'world']
re.split
执行以下操作:
分割得到的图案, 返回包含所产生的子列表的发生源字符串。
\w
和\W
是:
\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.
我建议使用'\ W +',因为它更通用。例如,如果你有“伦敦,英国”,它会返回'['伦敦','美国','王国']。如果没有'+',它会返回:'['London','','United','Kingdom']'(注意多余的空字符串)。 –
他们是不一样的,他们是对立的。显示输入字符串以使问题更清晰 – RomanPerekhrest