Dennis O'Keeffe
2 min readSep 4, 2021
Heading image

This is Day 14 of the #100DaysOfPython challenge

This post will use the regular expressions module from the standard library to … .

Prerequisites

  1. Familiarity with Pipenv. See here for my post on Pipenv.
  2. Familiarity with JupyterLab. See here for my post on JupyterLab.
  3. Familiarity with Regular Expressions

Getting started

Let’s create the hello-python-regex directory and install Pillow.

At this stage, we can start up the notebook server.

The server will now be up and running.

Creating the notebook

Once on http://localhost:8888/lab, select to create a new Python 3 notebook from the launcher.

Ensure that this notebook is saved in hello-python-regex/docs/regex.ipynb.

We will explore the following in each cell of the notebook:

  1. Importing the Regex module.
  2. A basic usage of the Regex module.
  3. String replacement with the Regex module.

Importing the module

This imports the regex module from the standard library.

Basic usage of the module

There are a number of useful module methods that we can use that we will demonstrate:

  1. Searching strings.
  2. Matching strings.
  3. Usage without compile.
  4. Splitting a string into a list.
  5. Replacing matches.

Searching strings

Scan through string looking for the first location where this regular expression produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

Matching strings

If zero or more characters at the beginning of string match this regular expression, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.

Usage without compile

When you use re.match and re.search as a static method, you can pass the Regex as the first argument:

Splitting a string into a list

Replacing matches

We can make use of the search and sub methods to replace matches.

For a more specific replacement (particularly in a large set of text), we can be more explicit with the string to replace:

Summary

Today’s post demonstrated how to use the re module from the standard library to search, match, split and replace text in Python strings.

This can be unbelievably useful when working with text files.

Resources and further reading

  1. The ABCs of Pipenv
  2. Hello, JupyterLab
  3. Pipenv
  4. Regular Expressions in Python
  5. Info on Regular Expressions
  6. Regex 101 — website for testing Regex expressions

Photo credit: pawel_czerwinski

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.

Dennis O'Keeffe
Dennis O'Keeffe

No responses yet