最初、中間、および/または最後に少なくとも1つの句読点(またはスペース以外の英数字以外の文字)が含まれるすべての単語を取得しようとしています。たとえば、この文では
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']
これにより、少なくとも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