Regular Expression Basics

Import the regex (re) package

import re

Import sys

import sys

Create a simple text string.

text = 'The quick brown fox jumped over the lazy black bear.'

Create a pattern to match

three_letter_word = '\w{3}'

Convert the string into a regex object

pattern_re = re.compile(three_letter_word); pattern_re
re.compile(r'\w{3}', re.UNICODE)

Does a three letter word appear in text?

re_search ='..own', text)

If the search query is at all true,

if re_search:
    # Print the search results


re.match() is for matching ONLY the beginning of a string or the whole string For anything else, use

Match all three letter words in text

re_match = re.match('..own', text)

If re_match is true, print the match, else print “No Matches”

if re_match:
    # Print all the matches
    # Print this
    print('No matches')
No matches


Split up the string using “e” as the seperator.

re_split = re.split('e', text); re_split
['Th', ' quick brown fox jump', 'd ov', 'r th', ' lazy black b', 'ar.']


Replaces occurrences of the regex pattern with something else

The “3” references to the maximum number of substitutions to make.

Substitute the first three instances of “e” with “E”, then print it

re_sub = re.sub('e', 'E', text, 3); print(re_sub)
ThE quick brown fox jumpEd ovEr the lazy black bear.