Regular Expressions Python Tutorial

Python PictureIn this video, I provide a bunch of examples on how to use Regular Expressions in Python. I show you how to search for numerous different types of data and output and manipulate that data.

I’ve provided all of the code after the video and if you have any questions, leave them in the comment section below.

In the next article, I’ll show you how to grab information from any web page and do what ever you want with that information.

Special Note: Click the HD and Full Screen buttons for the best view of the video.


Here is All of the Code Used in the Video

f = open(‘randomcharacters.txt’)

strToSearch =””

for line in f:
strToSearch += line


patFinder2 = re.compile(‘aa1B’, re.IGNORECASE)
findPat2 =,strToSearch)
if (findPat2):
else: print(“Pattern Not Found”)

patFinder3 = re.compile(‘.*’, re.IGNORECASE)
findPat3 = re.findall(patFinder3,strToSearch)

for i in findPat3:

patFinder4 = re.compile(‘[a-z]’, re.IGNORECASE)
findPat4 = re.findall(patFinder4,strToSearch)

for i in findPat4:
print(i, end=”)


patFinder5 = re.compile(‘[#$\*/\?\-0-9]’, re.IGNORECASE)
findPat5 = re.findall(patFinder5,strToSearch)

for i in findPat5:
print(i, end=”)


patFinder6 = re.compile(‘\d\D\s\S.\W\w’, re.IGNORECASE)
findPat6 = re.findall(patFinder6,strToSearch)

for i in findPat6:
print(i, end=”)


patFinder7 = re.compile(‘\d(\D\s\S.)(\W\w)’, re.IGNORECASE)
findPat7 =,strToSearch)



g = open(‘names.txt’)

strToSearch =””

for line in g:
strToSearch += line


patFinder8 = re.compile(‘(Je[nnifer|nny|n]{1,6}\s\w+\s)’, re.IGNORECASE)
findPat8 = re.findall(patFinder8,strToSearch)

for i in findPat8:

f = open(‘randomtext.txt’)

strToSearch =””

for line in f:
strToSearch += line

# Creating a pattern for pi
piFinder = re.compile(‘(\d\.\d*)\w’)

# Search the string for the pattern I created
findPi =,strToSearch)

print( # Show the value that matched the pattern

# Create a pattern to find a phone number
phNumFinder = re.compile(‘([0-9]{3}-?[0-9]{3}-?[0-9]{4})|(\(?[0-9]{3}\)?[0-9]{3}-?[0-9]{4})’)

findPhNum =  re.findall(phNumFinder,strToSearch)

for i in findPhNum:


2 Responses to “Regular Expressions Python Tutorial”

  1. Adil M. says:

    Ok, I am really getting stumped and need some help here…following are lines of code, where I would like to extract a ‘block’ of code that are between the “style” code. Any simple way of doing this? The example only shows it against one line of text.

    input type=”hidden” name=”__e” id=”__e” value=”/wEW>

    font-size: 20px;
    line-height: 28px;
    font-size: 13px;
    line-height: 20px;


  1. Tweets that mention Regular Expressions Python Tutorial | New Think Tank -- - [...] This post was mentioned on Twitter by Marcel Caraciolo, Python UK. Python UK said: Regular Expressions Python Tutorial |…

Leave a Reply

Your email address will not be published.