我试图在开头,中间和/或结尾处获得其中包含至少1个标点符号(或任何非空格,非字母数字字符)的所有单词.例如,在这句话中
this is a wo!rd right !and| other| hello |other
Run Code Online (Sandbox Code Playgroud)
正则表达式将返回
wo!rd !and| other| |other
Run Code Online (Sandbox Code Playgroud)
你可以用这个:
>>> 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']
Run Code Online (Sandbox Code Playgroud)
这将找到所有那些至少包含1 non-word, non-space字符的单词.\S和...一样[^\s].
正则表达式说明:
\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
Run Code Online (Sandbox Code Playgroud)