Alternation in Python: A Detailed Guide to Regular Expressions


Regular expressions are a powerful tool for pattern matching in Python. They allow developers to search, replace, and manipulate text strings with ease. One of the most important features of regular expressions is alternation, which allows developers to match one of several possible patterns at a time.

Explanation of What Alternation in Python Is

Alternation is a feature of regular expressions that allows developers to specify multiple possible patterns within a single regular expression. It works by separating the patterns with the vertical bar “|” character.

When a regular expression containing an alternation is matched against a string, each pattern within the alternation is tested in turn until one is successful. For example, suppose we want to match either “cat” or “dog” in a string.

We could use the following regular expression: “cat|dog”. If the string contains “cat”, the first pattern will be matched and if it contains “dog”, then the second pattern will be matched.

Brief Overview of Topics Covered

This guide will provide an in-depth look at how alternation works in Python regular expressions. We will cover everything from understanding what alternation is and how it works, to advanced techniques for using it effectively. In addition to basic syntax and examples, we’ll also look at how to use alternations with character sets and discuss common mistakes you should avoid when working with them.

By mastering this essential feature of Python’s regular expression library, you will be able to write more complex and powerful searches that can match multiple possibilities simultaneously with ease. So let’s get started exploring everything there is to know about using alternations in your Python code!

Understanding Alternation

Alternation is an important concept in regular expressions, and it involves matching one of several possible expressions. In Python, alternation is achieved by using the vertical bar symbol (`|`) to separate the different patterns that we want to match.

The regex engine then matches any part of the string that matches any of those patterns. For example, consider a search for all occurrences of either “cat” or “dog” in a given string.

We can use alternation to achieve this by writing the regular expression as `cat|dog`. The regex engine will then match either “cat” or “dog”, depending on which one appears in the string.

Examples of different use cases for alternation

Alternation can be used in many different ways, depending on the specific requirements of the pattern you are trying to match. Here are some examples:

– Matching variations of a word: Alternation can be used to match variations of a word with similar meanings or spellings. For example, you could search for both “colour” and “color” by using the regular expression `colou?r`.

– Matching alternative patterns: Alternation can be used to search for multiple patterns at once. For example, you could search for either a phone number or an email address by using the regular expression `(\d{3}-\d{3}-\d{4})|(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)`.

– Matching optional characters: Alternation can be used with optional quantifiers (such as `?`) to match optional characters or groups. For example, you could search for both “grey” and “gray” by using the regular expression `gr[ae]y`.

Understanding how alternation works in Python regular expressions is an important skill for any developer or data analyst. By using alternation, you can match multiple patterns at once and create more flexible and powerful regular expressions.

Alternation Syntax

The syntax for alternation in Python regular expressions involves using the vertical bar “|” symbol to separate the different options to be matched. The expression will match any of the options separated by the “|”.

For example, the regular expression “dog|cat” will match either “dog” or “cat”. One important thing to note about using alternation in Python regular expressions is that it only matches a single pattern.

If you want to match more than one pattern with alternation, you need to enclose them in parentheses. For example, the expression “(dog|cat) food” would match both “dog food” and “cat food”.

Another useful feature of using alternation in Python regular expressions is that it can be combined with other regex syntax elements like character classes and quantifiers. For example, the expression “\d{3}-\d{4}|N/A” would match either a phone number with a specific format (e.g. 555-1234) or “N/A”.

Example: Matching Email Addresses

Here’s an example of how to use alternation effectively in Python regular expressions when trying to match email addresses: import re

text = “Contact us at [email protected] or [email protected]” pattern = r”\b\w+@\w+\.[a-zA-Z]{2,3}\b”

matches = re.findall(pattern, text) print(matches)

In this code snippet, we’re searching for email addresses within a larger body of text. We’ve defined our pattern as “\b\w+@\w+\.[a-zA-Z]{2,3}\b”, which uses alternation with the “|” symbol to match two different types of email address endings – either “.com” or “.edu”. When this code is run on our sample text string, it will return a list of matches that includes both “in[email protected]” and “[email protected]”.

Example: Matching Phone Numbers

Alternation can also be combined with character classes to match specific patterns like phone numbers. Here’s an example:

import re

text = "Please call 555-1234 or 555-5678 for assistance." pattern = r"\d{3}-\d{4}|\d{3}\.\d{4}"

matches = re.findall(pattern, text) print(matches)

In this code snippet, we’ve defined our pattern as “\d{3}-\d{4}|\d{3}\.\d{4}”, which uses alternation to match phone numbers in two different formats – either separated by a hyphen or a period. When this code is run on our sample text string, it will return a list of matches that includes both “555-1234” and “555-5678”.

Using Alternation with Character Sets

When it comes to regular expressions, character sets are an essential tool for matching specific patterns of text. They allow us to define a group of characters that should be matched in a specific position. When combined with alternation, character sets become even more powerful and flexible.

Match Phone Numbers with Alternation and Character Sets

One common use case for alternation in combination with character sets is matching phone numbers. Phone numbers can take many different forms, including various formats of the area code, the use of dashes or parentheses between groups of digits, and even different numbers of digits in each group depending on the country or region. To match any phone number using alternation and character sets, we can start by defining a set of characters that represent all possible digits: [0-9].

We can then combine this set with alternation to create patterns that match all possible phone number formats. For example:

python import re

pattern = r'\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}' text = "Call me at (123) 456-7890"

match =, text) if match:


This pattern matches phone numbers that have optional parentheses around the area code followed by either a dash, dot or whitespace before the next set of digits and another separator before the last set.

Email Addresses Matching using Alternation

Another great example where we can use alternations and character sets is email addresses matching. Email addresses consist mostly consist two parts; [email protected] where username part contains alphanumeric characters while domain name consists mainly alphabetical characters or hyphens (-) separated by dots (.). For matching an email address pattern using alternation, we can start by creating a character set for the username part containing all the alphanumeric characters.

To match the domain name, we can create another character set containing the alphabetical characters, hyphens and dots. The alternation operator can then be used to combine these two parts:

python import re

pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' text = "Email me at [email protected]"

match =, text) if match:


This pattern matches email addresses with alphanumeric usernames separated by a dot followed by an alphabetical domain name separated by a dot and ending with 2 or more alphabets.

Combining alternation with character sets in Python regular expressions allows us to create powerful patterns that can match complex text patterns such as phone numbers and email addresses. By developing a deep understanding of both concepts we can write more effective regular expressions that will help us extract valuable data from unstructured text sources.

Advanced Techniques with Alternation

The Power of Lookahead and Lookbehind Assertions

Lookahead and lookbehind assertions are some of the most advanced techniques that can be used in combination with alternation in Python Regular Expressions. They allow you to create more specific expressions by matching a pattern with another pattern either before (lookbehind) or after (lookahead) it, without including it in the final match.

For instance, if we wanted to match only the word “python” that appears immediately before the word “language,” we could use a positive lookahead assertion as follows: `python(?=\s+language)`

In this example, `(?=\s+language)` is the positive lookahead assertion that tells Python to find any occurrences of `python` followed by one or more whitespaces and then followed by `language`, without actually including `language` in the final match. Similarly, if we wanted to match only the word “up” that appears immediately after the word “look,” we could use a positive lookbehind assertion as follows:

`(?<=look\s+)up` Here, `(?<=look\s+)` is our positive lookbehind assertion that matches any occurrences of ‘up’ preceded by ‘look’ and at least one whitespace character.

Practical Examples for Using Lookahead and Lookbehind Assertions

Let’s say you have a list of email addresses containing different domain names such as Gmail, Yahoo, Outlook etc., but you only want to match email addresses from Gmail domain. You can accomplish this using a combination of alternation and lookahead assertions as shown below:

`\b\w+\b@gmail\.com(?=[^\w]|\b)` In this expression, `\b\w+\b@gmail\.com` matches any email address ending with while `(?=[^\w]|\b)` is our positive lookahead assertion that ensures the email address is not followed by any other character except for non-word characters or word boundaries.

Another practical example of using lookahead and lookbehind assertions would be to match phone numbers that follow a specific pattern such as `+234-1234567`. The following expression can be used to match only the digits in such phone numbers:

`(?<=\+234-)\d{7}` In this expression, `(?<=\+234-)` is our positive lookbehind assertion that matches any occurrences of 7-digit phone numbers preceded by “+234-” string.

Mastering alternation in Python regular expressions is essential for anyone who wants to write efficient code. Understanding more advanced techniques such as lookahead and lookbehind assertions can help you create more specific expressions and make your code even more precise. Although these techniques require a little bit of practice, once you understand how they work, they will help take your regex skills to the next level!

Common Mistakes to Avoid

When working with alternation in Python regular expressions, there are several common mistakes that developers may make. These mistakes can result in inefficient code, incorrect matches, and even crashes. Here, we’ll discuss some of the most common mistakes made when using alternation and provide tips on how to avoid them.

Overusing Alternation

One of the most common mistakes when using alternation is overusing it. Alternation can be a powerful tool for matching multiple patterns at once, but using too many alternatives can slow down your code and make it difficult to read and debug. The more alternatives you have in your expression, the longer it will take for Python’s regex engine to evaluate your pattern.

To avoid this mistake, it’s important to carefully consider whether each alternative is necessary before including it in your expression. Try to keep your expressions as simple as possible by only including the necessary alternatives.

Misunderstanding Precedence

Another common mistake when using alternation is misunderstanding operator precedence. In Python regular expressions, parentheses can be used to group alternatives together and control operator precedence. However, if parentheses are not used correctly or omitted entirely, the order of operations can become confusing and lead to incorrect matches.

To avoid this mistake, always use parentheses to group alternative patterns together where necessary. This will ensure that the correct order of operations is followed and prevent any confusion or errors.

Not Escaping Special Characters

A final common mistake when using alternation is not escaping special characters properly. In Python regular expressions, certain characters have special meanings and must be escaped with a backslash (\) if they are meant to be matched literally. For example, the dot (.) character matches any character except a newline unless it is escaped with a backslash.

To avoid this mistake, always make sure to escape special characters that are meant to be matched literally. This will prevent any unexpected matches and ensure that your pattern is evaluating as expected.


Recap of Key Points Covered Throughout the Guide

Throughout this guide, we have covered a variety of topics related to alternation in Python regular expressions. We started by defining alternation and discussing its importance in creating patterns that match specific strings.

We then explored the syntax used for alternation in Python, including how it can be combined with character sets to match more complex patterns. From there, we delved into more advanced techniques like lookahead and lookbehind assertions.

Throughout each section, we provided numerous examples and sample code to illustrate key concepts. By following along with these examples and practicing on your own, you can gain a deep understanding of how alternation works in Python regular expressions.

Final Thoughts on the Importance of Mastering Alternation in Python Regular Expressions

As you continue to develop your skills as a programmer, understanding regular expressions is crucial for working with text data. Alternation is an important tool that can help you construct complex patterns that match specific strings or sets of strings.

By mastering alternation in Python regular expressions, you will be better equipped to work with all kinds of text data – from phone numbers and email addresses to web pages and other online content. With practice and dedication, you can become proficient at using alternation (and other regular expression tools) to effectively manipulate text data.

While mastering alternation may seem daunting at first, with time and practice it can become an essential part of your toolkit as a programmer. So take what you’ve learned here today, continue practicing on your own, and soon enough you’ll be an expert at using alternation (and regular expressions more broadly) in your Python code!

Related Articles