Regex in Python Examples

Example 1: Matching Hello, World!

import re
pattern = re.compile('hello')
if re.match(pattern, 'hello world')
print("Hello, world!")

Example 2: Finding Words

import re
text = 'The quick brown fox jumped over the lazy dog.'
words = re.findall(r'\b\w+\b', text)
print(words)

More Examples: