How to find all the hyphenated words in a string using regex in Python

1 Answer

0 votes
import re

s = 'off-campus python state-of-the-art java 500-525 php'

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

print(r)

 
'''
run:
 
['off-campus', 'state-of-the-art', '500-525']
 
'''

 



answered Jul 31, 2019 by avibootz
...