正则表达式标点分割 [Python]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19894478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:54:39  来源:igfitidea点击:

Regex punctuation split [Python]

pythonregexstringsplitpunctuation

提问by dantdj

Can anyone help me a bit with regexs? I currently have this: re.split(" +", line.rstrip()), which separates by spaces.

任何人都可以帮助我使用正则表达式吗?我目前有这个: re.split(" +", line.rstrip()),用空格分隔。

How could I expand this to cover punctuation, too?

我该如何扩展它以涵盖标点符号?

采纳答案by Mister_Tom

The official Python documentation has a good example for this one. It will split on all non-alphanumeric characters (whitespace and punctuation). Literally \W is the character class for all Non-Word characters. Note: the underscore "_" is considered a "word" character and will not be part of the split here.

官方 Python 文档对此有一个很好的示例。它将拆分所有非字母数字字符(空格和标点符号)。字面上的 \W 是所有非单词字符的字符类。注意:下划线“_”被认为是一个“单词”字符,不会成为这里拆分的一部分。

re.split('\W+', 'Words, words, words.')

See https://docs.python.org/3/library/re.htmlfor more examples, search page for "re.split"

有关更多示例,请参阅https://docs.python.org/3/library/re.html,搜索页面“re.split”

回答by Ashwini Chaudhary

Using string.punctuationand character class:

使用string.punctuation和字符类:

>>> from string import punctuation
>>> r = re.compile(r'[\s{}]+'.format(re.escape(punctuation)))
>>> r.split('dss!dfs^  #$% jjj^')
['dss', 'dfs', 'jjj', '']

回答by dawg

import re
st='one two,three; four-five,    six'

print re.split(r'\s+|[,;.-]\s*', st)
# ['one', 'two', 'three', 'four', 'five', 'six']