Python Regex for hyphenated words

后端 未结 1 1889
失恋的感觉
失恋的感觉 2021-01-11 15:39

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-thre         


        
1条回答
  •  轮回少年
    2021-01-11 16:03

    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

    0 讨论(0)
提交回复
热议问题