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

Import the regex (re) package


In [1]:
import re

Import sys


In [2]:
import sys

Create a simple text string.


In [3]:
text = 'The quick brown fox jumped over the lazy black bear.'

Create a pattern to match


In [4]:
three_letter_word = '\w{3}'

Convert the string into a regex object


In [5]:
pattern_re = re.compile(three_letter_word); pattern_re


Out[5]:
re.compile(r'\w{3}', re.UNICODE)

Does a three letter word appear in text?


In [6]:
re_search = re.search('..own', text)

If the search query is at all true,


In [7]:
if re_search:
    # Print the search results
    print(re_search.group())


brown

re.match

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

Match all three letter words in text


In [8]:
re_match = re.match('..own', text)

If re_match is true, print the match, else print "No Matches"


In [9]:
if re_match:
    # Print all the matches
    print(re_match.group())
else:
    # Print this
    print('No matches')


No matches

re.split

Split up the string using "e" as the seperator.


In [10]:
re_split = re.split('e', text); re_split


Out[10]:
['Th', ' quick brown fox jump', 'd ov', 'r th', ' lazy black b', 'ar.']

re.sub

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


In [11]:
re_sub = re.sub('e', 'E', text, 3); print(re_sub)


ThE quick brown fox jumpEd ovEr the lazy black bear.