0

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

this is a wo!rd right !and| other| hello |other

正規表現は返されます

wo!rd !and| other| |other
4

1 に答える 1

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']

これにより、少なくとも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
于 2013-02-14T12:44:18.753 に答える