Saturday, July 2, 2022

Python - Regular Expressions

# Import the regular expression module
import re

txt = "The rain in Spain"

# This searches for:
# ^The = "The" at the beginning of the line (^)
# Spain$ = "Spain" at the end of the line ($)
x = re.search("^The.*Spain$", txt)

print(x)

# Pretty much any populated variable evaluates to True
# So if we got a match, that value is tucked into x
# Which means this will evaluate to True if we got a match
if bool(x) == True:
    print("We got a match")

else:
    print("We did not geta match")

# Split returns a list where the string has been split at each match
# So the next line returns all the words separated by a space:
# The, rain, in, Spain
words = re.split(" ", txt)
print(words)

# Sub replaces one or many matches with a string
new = re.sub("Spain", "England", txt)
# The rain in England
print(new)

# Review regular expression matching
# A quick review sheet is here:
# https://www.w3schools.com/python/python_regex.asp