
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How Regular Expression Alternatives Work in Python
Regular expressions are a very crucial part of Python's built-in 're' module. It is used to work with text data. One of the advantages of regex is the ability to use alternatives, which allows you to match one pattern out of multiple options.
Alternatives in the regular expressions function are just like "or" statements. They help you to define many patterns to match against a given string. The vertical bar | denotes the "or" operator in regex.
For example, the regex pattern PHP|Python will match either "PHP" or "Python" in a given string. The following are the 4 different ways to accomplish this task -
Simple Alternative
Let us begin with a basic example in which we want to find either "php" or "python" in a string. In the program below, we will look for the words "php" or "python" in the text. The re.findall() function returns a list of found matches.
Example
Here is the program for showing the simple alternative -
import re # Define the string text = "I work on php projects but i also like python." # Use regex pattern = r'php|python' matches = re.findall(pattern, text) print(matches)
This will produce the following result -
['php', 'python']
Case-Insensitive Matching
As you can see in the above example, we have used only lowercase letters, we will enhance our example to match both lowercase and uppercase letters of the words. So the flags=re.IGNORECASE argument will help us to achieve this task.
Example
Here is an example to perform the case-insensitive matching -
import re # Define the string text = "I work on PHP projects but i also like Python." # Use regex here pattern = r'php|python' matches = re.findall(pattern, text, flags=re.IGNORECASE) print(matches)
Running the code will produce the following output -
['PHP', 'Python']
Matching Multiple Alternatives
Now let us see the pattern to match multiple programming languages like PHP, Python, Java, or HTML. The pattern we will use lists multiple alternatives separated by the " | " operator, and we will also use the re.findall(), which is used to return all occurrences found in the string.
Example
Here is an example for matching multiple alternatives -
import re # Define the string text = "I enjoy coding with php, python, java, and html." # Use regex pattern = r'php|python|java|html' matches = re.findall(pattern, text) print(matches)
Running the code will produce the following output -
['php', 'python', 'java', 'html']
Find Multiple File Extensions
Suppose we want to find all files in a directory with the .jpg or .jpeg extension. So we will use the vertical bar | symbol to create a unique pattern in our regular expression.
Example
In this example, the regular expression pattern r'.(jpg|jpeg)$' is used to match any file that ends with .jpg or .jpeg.
import re files = ["file.jpg", "file.jpeg", "file.txt"] for file in files: if re.search(r'\.(jpg|jpeg)$', file): print(f"Found a match: {file}")
This will lead to the following outcome -
Found a match: file.jpg Found a match: file.jpeg