Skip to main content

Python RegEx

A regular expression, or RegEx for short, is a series of characters that describes a search pattern.

With one, you can test whether a string contains that pattern.


RegEx Module

Regular expressions in Python are handled by re, a built-in package.

Start by importing it:

Example

main.py
import re

RegEx in Python

Once re is imported, regular expressions are ready to use:

Example

Check whether the string begins with "The" and finishes with "Spain":
Prompt
Import the re module and search a string to see if it starts with "The" and ends with "Spain". Use re.search() with the pattern "^The.*Spain$".
Output: complete Python program

RegEx Functions

To look for matches in a string, the re module gives you these functions:

  • findall - Gives back a list of every match
  • search - Gives back a Match object when a match exists anywhere in the string
  • split - Cuts the string at every match and gives back a list of the pieces
  • sub - Swaps one or many matches for a string of your choice

Metacharacters

A metacharacter is a character that carries a special meaning:

  • [] - Defines a set of characters
  • \ - Starts a special sequence, or escapes a special character
  • . - Matches any character other than a newline
  • ^ - Matches at the start
  • $ - Matches at the end
  • * - Zero or more repeats
  • + - One or more repeats
  • ? - Zero or one repeat
  • {} - The exact number of repeats you specify
  • | - One option or the other
  • () - Captures and groups

Flags

Flags added to a pattern change how a regular expression behaves.

  • re.ASCII (re.A) - Matches ASCII characters only
  • re.DEBUG - Shows debug information
  • re.DOTALL (re.S) - Lets the . character match everything, newlines included
  • re.IGNORECASE (re.I) - Ignores upper and lower case when matching
  • re.MULTILINE (re.M) - Matches at the start and end of every line
  • re.NOFLAG - Says that this pattern has no flag set
  • re.UNICODE (re.U) - Gives Unicode matches
  • re.VERBOSE (re.X) - Permits whitespace and comments inside a pattern

Special Sequences

Put a \ in front of certain characters and you get a special sequence, which has its own special meaning. These are the ones available:

  • \A - Finds a match only when the given characters open the string
  • \b - Finds a match when the given characters begin or end a word
  • \B - Finds a match when the given characters appear, but NOT at the beginning or end of a word
  • \d - Finds a match for any digit (0-9) in the string
  • \D - Finds a match for anything in the string that is NOT a digit
  • \s - Finds a match for any white space character in the string
  • \S - Finds a match for anything in the string that is NOT white space
  • \w - Finds a match for any word character in the string
  • \W - Finds a match for anything in the string that is NOT a word character
  • \Z - Finds a match only when the given characters close the string

Sets

A set is a group of characters between square brackets [], and it too has a special meaning:

  • [arn] - Matches one of the listed characters: a, r or n
  • [a-n] - Matches any lowercase letter that falls between a and n in the alphabet
  • [^arn] - Matches any character other than a, r and n
  • [0123] - Matches one of the listed digits: 0, 1, 2 or 3
  • [0-9] - Matches any single digit from 0 through 9
  • [0-5][0-9] - Matches any two-digit number from 00 through 59
  • [a-zA-Z] - Matches any letter from a to z, whether lower case OR upper case
  • [+] - Inside a set, the characters +, *, ., |, (), $ and {} lose their special meaning, so [+] just looks for a literal + in the string

The findall() Function

The findall() function collects all the matches into a list, which keeps them in the order they were found.

When nothing matches, you get an empty list:

Example

Show every match as a list:
Prompt
Use re.findall() to find all occurrences of "ai" in "The rain in Spain". Print the list of matches.
Output: complete Python program

Example

With no match to collect, the list comes back empty:
Prompt
Use re.findall() to search for "Portugal" in "The rain in Spain". Print the result (empty list when no match).
Output: complete Python program

The search() Function

The search() function looks through the string and, when it finds a match, gives you a Match object.

Several matches in the string make no difference, because you only get the first one:

When nothing matches, you get the value None:

Example

Find where the first white-space character sits in the string:
Prompt
Use re.search() to find the first white-space character in "The rain in Spain". Print its position using .start().
Output: complete Python program

Example

Run a search that finds nothing:
Prompt
Use re.search() to search for "Portugal" in "The rain in Spain". Print the result (None when no match).
Output: complete Python program

The split() Function

The split() function breaks the string at each match and returns the pieces in a list:

The maxsplit parameter limits how many splits are made:

Example

Break the string at every white-space character:
Prompt
Use re.split() to split "The rain in Spain" at each white-space character. Print the resulting list.
Output: complete Python program

Example

Break the string at the first white-space character only:
Prompt
Use re.split() to split "The rain in Spain" at the first occurrence only (maxsplit=1). Print the result.
Output: complete Python program

The sub() Function

The sub() function puts text of your choosing in place of each match:

The count parameter limits how many replacements are made:

Example

Put the number 9 wherever there is a white-space character:
Prompt
Use re.sub() to replace every white-space with "9" in "The rain in Spain". Print the result.
Output: complete Python program

Example

Change only the first 2 white-space characters:
Prompt
Use re.sub() to replace the first 2 white-space characters with "9" in "The rain in Spain". Print the result.
Output: complete Python program

Match Object

A Match Object holds details about a search and what it found.

Note: When nothing matches, you get the value None and no Match Object at all.

To get at those details, use the object's properties and methods:

  • .span() gives a tuple holding where the match starts and where it ends
  • .string gives back the string you passed to the function
  • .group() gives back the matching text

Example

Run a search that produces a Match Object:
Prompt
Use re.search() to search for "ai" in "The rain in Spain". Print the Match object returned.
Output: complete Python program

Example

Show the start and end positions of the first match:
Prompt
Search for a word starting with "S" using r"\bS\w+" in "The rain in Spain". Print the span of the match using .span().
Output: complete Python program

Example

Show the string that was given to the function:
Prompt
Search for a word starting with "S" using r"\bS\w+" in "The rain in Spain". Print the original string using .string.
Output: complete Python program

Example

Show just the matching text:
Prompt
Search for a word starting with "S" using r"\bS\w+" in "The rain in Spain". Print the matched text using .group().
Output: complete Python program