Python 正则表达式以捕获word中至少1个标点字符

Python 正则表达式以捕获word中至少1个标点字符,python,regex,Python,Regex,我试图得到所有单词,其中至少有一个标点符号或任何非空格,非字母数字字符的开头,中间和/或结尾。例如,在这句话中 this is a wo!rd right !and| other| hello |other 正则表达式将返回 wo!rd !and| other| |other 您可以使用以下选项: >>> sentence = "this is a wo!rd right !and| other| hello |other" >>> import re

我试图得到所有单词,其中至少有一个标点符号或任何非空格,非字母数字字符的开头,中间和/或结尾。例如,在这句话中

this is a wo!rd right !and| other| hello |other
正则表达式将返回

wo!rd !and| other| |other
您可以使用以下选项:

>>> sentence = "this is a wo!rd right !and| other| hello |other"

>>> import re

>>> re.findall("\S*[^\w\s]\S*", sentence)
['wo!rd', '!and|', 'other|', '|other']
这将查找所有这些单词,其中至少包含一个非单词、非空格字符\S与[^\S]相同

正则表达式解释:


@索菲亚。是 啊它将只匹配和| in!和|!将匹配\S但不匹配\w。
\S*      # Match 0 or more non-space character
[^\w\s]  # Match 1 non-space non-word character
\S*      # Match 0 or more non-space character