Reputation: 10696
I am trying to get all words that have inside them at least 1 punctuation mark (or any non space, non alphanumeric character) in the beginning, middle and/or end. So for example, in this sentence
this is a wo!rd right !and| other| hello |other
the regex would return
wo!rd !and| other| |other
Upvotes: 0
Views: 2454
Reputation: 213223
You can use this:
>>> 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']
This will find all those words, containing at least 1 non-word, non-space
character. \S
is same as [^\s]
.
Regex Explanation:
\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
Upvotes: 8