正则表达式在单词中捕获至少1个标点字符

Sof*_*fia 0 python regex

我试图在开头,中间和/或结尾处获得其中包含至少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)

Roh*_*ain 8

你可以用这个:

>>> 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)