Skip to content Skip to sidebar Skip to footer

Python Regex For Hyphenated Words

I'm looking for a regex to match hyphenated words in python. The closest I've managed to get is: '\w+-\w+[-w+]*' text = 'one-hundered-and-three- some text foo-bar some--text' hyphe

Solution 1:

Try this:

re.findall(r'\w+(?:-\w+)+',text)

Here we consider a hyphenated word to be:

  • a number of word chars
  • followed by any number of:
    • a single hyphen
    • followed by word chars

Post a Comment for "Python Regex For Hyphenated Words"