“Or” Logic In Regular Expression (Regex)
Boolean Logic provides different operators where “or” operator is one of them. The regular expressions are used to match specific patterns by providing characters. The “or” operator can be used to provide options to match where one of the options should match provided with the “or” operator. The “or” operator is also called as “or” logic too. In this tutorial, we examine how to use “or” logic/operator with the regular expressions (regex).
“Or” Logic Operator
The “|” is used as or logic operator. Multiple terms are provided by delimiting with the “|”. It is expected to match one of the provided terms. If one of the terms match the regular expression returns the matched part. The syntax of the “Or” logic is like below. Parenthesis is used to group terms to match as a whole.
“Or” Logic with Regex
Let’s make a simple example where we check if one of the provided names matches the list of names. In the following example, we use (Ahmet|John|Sandra) to match one of them.
From the output, we can see that there is a single match which is “Ahmet”. Others are not matched as they are not provided with the “Or” logic.
“Or” Logic with Python Regex
The previous “or” logic example can be implemented in the Python programming language. Python provides the “re” module for regular expression operations. So first we import the “re” module and then use the “findall()” method to match “or” logic.
import re text = """Ahmet Mehmet Elif İsmail""" re.findall('(Ahmet|John|Sandra)')
Python Regex Or – A Simple Illustrated Guide
This tutorial is all about the or | operator of Python’s re library. You can also play the tutorial video while you read:
Want to earn money while you learn Python? Average Python programmers earn more than $50 per hour. You can certainly become average, can’t you?
Join the free webinar that shows you how to become a thriving coding business owner online!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.
Be on the Right Side of Change 🚀
- The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. 🤖
- Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
- Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.
Learning Resources 🧑💻
⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!
Join the Finxter Academy and unlock access to premium courses 👑 to certify your skills in exponential technologies and programming.
New Finxter Tutorials:
Finxter Categories: