Regular Expressions

Written by Ferhawn Shaheen (10/2/2020)

Introduction

Regular expressions, or regex, are a way of easily searching for specific strings within text. This is very important when one is working with larger and more complex programs. It consists of several characters that can be used in tandem to enter a specific query. This lesson will cover the basics of regex.

Definition and a Basic Example

Regexing specifically uses patterns with characters and sequences that can yield a specific result throughout the text. The following is a basic example.

import re

# Check if the string starts with "The" and ends with "Spain":

txt = "The rain in Spain"
match = re.search("^The.*Spain$", txt)

if match:
  print("YES! We have a match!")
else:
  print("No match.")

The re module must be imported to use regular expressions generally. A string is then defined followed by the most important statement, which uses re.search() to specify what is going to be searched for. The first argument in the function is the actual regex string. A simple if-else statement ensues, which works with the results of the re.search() function.

The regex string ^The.*Spain$ uses metacharacters, which are defined further in the following section.

  • The ^ means that the start of txt must match the regex, and not somewhere in the middle. E.g., the string "This is The Spain" won't be a match.

  • $ ensures that the end of txt must match the regex, and not somewhere else. E.g., the string"The king of Spain is a man" won't match.

  • The . and * are two different metacharacters. . means that there can be any single character after the characters T, h, and e. The * means "zero-or-more" occurrences of any characters. When combined with together in the form .*, this means that there can be any number of any characters.

    • E.g., "TheSpain", "TheASpain", "TheAAASpain", and "TheABCSpain" all match.

Metacharacters

Metacharacters hold a special meaning when using them. Here is a list of the ones that are used with regex.

Special Sequences

Special sequences are used in tandem with the \ metacharacter. They, like the metacharacters, have specific meanings.

Sets

Sets are a set of characters inside brackets that also denote a specific meaning.

Functions to Use

findall()

The findall() function returns a list of all the items that match the given specifications.

import re

txt = "The rain in Spain"
match = re.findall("ai", txt)
print(match)  # Returns ["ai", "ai"]

Here is another example. Note that an empty list is returned if a desired item does not exist.

import re

txt = "The rain in Spain"
match = re.findall("Portugal", txt)
print(match)  # Returns []

sub()

The sub() function replaces the matches with a text of choice.

import re

txt = "The rain in Spain"
match = re.sub("\s", "9", txt)
print(match)  # Returns "The9rain9in9Spain"

It is worth noting that count controls the number of substitutions.

import re

txt = "The rain in Spain"
match = re.sub("\s", "9", txt, 2)
print(match)  # Returns "The9rain9in Spain"

Important Note

This lesson contains a lot of material to work with. It is highly advised some time is spent to apply and learn what to do with all of the metacharacters, sequences, and sets. Of course, there is more to look into if one does online research.

This online regex tester is also very helpful when learning regex: https://regex101.com/

Conclusion

This lesson covered the basics of regex. It can be very helpful and useful throughout Python. There is certainly a lot of material to cover and learn, so doing more research and having some time to practice is very helpful.

Last updated