python中分割字符串两种方法正则分组别名,如何在python中使用正则表达式模块将文本字符串分割成单词?...
Here's what I'm working with…
string1 = "Dog,cat,mouse,bird. Human."
def string_count(text):
text = re.split('\W+', text)
count = 0
for x in text:
count += 1
print count
print x
return text
print string_count(string1)
…and here's the output…
1
Dog
2
cat
3
mouse
4
bird
5
Human
6
['Dog', 'cat', 'mouse', 'bird', 'Human', '']
Why am I getting a 6 even though there are only 5 words? I can't seem to get rid of the '' (empty string)! It's driving me insane.
解決方案
Because while it splits based on the last dot, it gives the last empty part also.
You splitted the input string based on \W+ which means split the input string based on one or more non-word character. So your regex matches the last dot also and splits the input based on the last dot also. Because of no string present after to the last dot, it returns an empty string after splitting.
總結
以上是生活随笔為你收集整理的python中分割字符串两种方法正则分组别名,如何在python中使用正则表达式模块将文本字符串分割成单词?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux如何判断网线插入_【干货】配线
- 下一篇: python中size的用法_在Pyth