Python – Use regex to split hyphenated words

I was asked this question on stackoverflow, and figured I’d repost it here since I wrote it after all.


s = "-this is. A - sentence;one-word what's"
re.findall("\w+-\w+|[\w']+",s)

result: ['this', 'is', 'A', 'sentence', 'one-word', "what's"]

make sure you notice that the correct ordering is to look for hypenated words first!

This entry was posted in Python, regex and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>