Skip to content Skip to sidebar Skip to footer

Python Regex For Finding All Words In A String

Hello I am new into regex and I'm starting out with python. I'm stuck at extracting all words from an English sentence. So far I have: import re shop='hello seattle what have you

Solution 1:

Use word boundary \b

import re

shop="hello seattle what have you got"
regex = r'\b\w+\b'
list1=re.findall(regex,shop)
print list1

OP : ['hello', 'seattle', 'what', 'have', 'you', 'got']

or simply \w+ is enough

import re

shop="hello seattle what have you got"
regex = r'\w+'
list1=re.findall(regex,shop)
print list1

OP : ['hello', 'seattle', 'what', 'have', 'you', 'got']

Post a Comment for "Python Regex For Finding All Words In A String"