Python – Regular Expressions

I finished the OOP lessons and boy were they much harder than expected, I went on to next set of lessons which is Regular Expressions. I was looking at the comments which is great because users will explain various code example in different ways, but many in the regex comments were saying ‘finally made it past OOP’ or ‘OOP survivors’ hah. Yeap it wasn’t only me thinking those lessons were tougher, I know those lessons will need to be gone over many times.

So anyway regex is always amazing, it’s something I do not use often, but have done lessons on in other lessons so I know how they work. Python regex is pretty damn good and so far not too hard to remember, below are some of my lesson code examples and practice from sololearn.

I find the last one to be a combination of stuff very useful to me, there are a bunch more lessons to go where the regex looks more complicated. The use of r’stuff’ is an awesome feature eliminating lots of backslashes you commonly see in regex.

import re

#Create a program that will take the phone number as input, 
# and if the number starts with "00", replace them with "+".
# The number should be printed after formatting.

num = input('Enter number')
pat = r'00'
if re.match(pat, num):
	newnum = re.sub(pat,'+',num)
	print(newnum)
else:
	print(num)

# Example metacharacter dot . matches any character in position

pattern = r"gr.y"
if re.match(pattern, "grey"):
    print("Match 1")
if re.match(pattern, "gray"):
    print("Match 2")
if re.match(pattern, "blue"):
    print("Match 3")

# The next two metacharacters are ^ and $
# These match the start and end of a string, respectively.
pattern = r"^gr.y$"
if re.match(pattern, "grey"):
    print("Match 1")
if re.match(pattern, "gray"):
    print("Match 2")
if re.match(pattern, "stingray"):
    print("Match 3")

# Test Practice
# Write a program that takes a word as input, and outputs "Match" if the word has 4 letters, 
# starts with "m" and ends with "e". The program should output "No match" if these mentioned 
# requirements aren't satisfied.

word = input('Enter a 4 letter word')
pattern = r'^m..e$'
if re.match(pattern,word):
    print('Match')
else:
    print('No match')

# Character classes []
pattern = r"[A-Z][A-Z][0-9]"

if re.search(pattern, "LS8"):
    print("Match 1")

if re.search(pattern, "E3"):
    print("Match 2")

if re.search(pattern, "1ab"):
    print("Match 3")
    

# ^ inverts meaning if not the pattern, re.search goes through entire pattern not just first char
pattern = r"[^A-Z]"
if re.search(pattern, "this is all quiet"):
    print("Match 1")
if re.search(pattern, "AbCdEfG123"):
    print("Match 2")
if re.search(pattern, "THISISALLSHOUTING"):
    print("Match 3")

# Test practice
# The first symbol: an uppercase character
# The second symbol: an uppercase character
# The third symbol: a digit
# The forth symbol: a digit

# Write a program for a search tool, 
# that will take the ID as input and output "Searching" if the format is correct, 
# and "Wrong format", if it's not. Note the $ ends the id so KJ09K90 would be wrong format
Id = input('Enter the id ')

#your code goes here
pattern = r'[A-Z][A-Z][0-9][0-9]$'
if re.search(pattern,Id):
    print('Searching')
else:
    print('Wrong format')    

Leave a Comment