How to split strings with multiple delimiters into words using regex in Python

3 Answers

0 votes
import re
 
s = "$Python  - programming, java, php!."

f = re.findall(r"[\w']+", s)

print(f)


  
'''
run:
  
['Python', 'programming', 'java', 'php']
  
'''

 



answered Aug 1, 2019 by avibootz
0 votes
import re
 
s = "$Python  - programming, java, php!."

words = re.split('\W+', s)

print(words)


  
'''
run:
  
['', 'Python', 'programming', 'java', 'php', '']
  
'''

 



answered Aug 1, 2019 by avibootz
0 votes
import re
  
s = "$Python  - programming, java, php!"
 
words = s.replace('$',' ').replace('-',' ').replace('!',' ').replace(',',' ').split()
 
print(words)
 
 
   
'''
run:
   
['Python', 'programming', 'java', 'php']
   
'''

 



answered Aug 1, 2019 by avibootz
...