Title: Regular Expression Basics
Slug: regular_expressions_basics
Summary: Regular Expression Basics
Date: 2016-05-01 12:00
Category: Python
Tags: Basics
Authors: Chris Albon
In [1]:
import re
In [2]:
import sys
In [3]:
text = 'The quick brown fox jumped over the lazy black bear.'
In [4]:
three_letter_word = '\w{3}'
In [5]:
pattern_re = re.compile(three_letter_word); pattern_re
Out[5]:
In [6]:
re_search = re.search('..own', text)
In [7]:
if re_search:
# Print the search results
print(re_search.group())
In [8]:
re_match = re.match('..own', text)
In [9]:
if re_match:
# Print all the matches
print(re_match.group())
else:
# Print this
print('No matches')
In [10]:
re_split = re.split('e', text); re_split
Out[10]:
In [11]:
re_sub = re.sub('e', 'E', text, 3); print(re_sub)