What is Python Regex?
Python RegEx is a sequence of characters that form a specific pattern and can be used as a tool for checking out patterns in strings.
Now we can set up the pattern to what we want and then we can use python to go through the string of text and match the words or whatever in that pattern that we’re after. Python RegEx is quite a vast subject and it is very useful in the world of programming and its basics as well.
In this article, we are going to use the search method, although there are a few other ones, but we will be using re.search(). Now since “re” is inbuilt into the standard Python library we need to import it into our workspace.
Import the Python RegEx module
Importing re allows us to access the search command as well as other methods in the library.
In this example, we are going to create a string object “here’s my phone number 555-555-555”. We are now going to use the re module, give it a pattern and then do a search and let it return a search object.
How to use Python RegEx?
Generally speaking, you might have more complicated patterns than this. We just need to give an r” ” and then we need to open our brackets and we are going to search in this text for the word “phone”.Now, there would be easy ways to do this in python as well, but it’s important to know that this is just the basics, then we do a comma and then we give it the string to search in. And finally, we can now print out the match object if it finds the pattern as shown in the code below.
The output displays the match object which is the string “phone” states the span in which one can locate them in the string. Now, what these 10 and 15 is it’s actually the index of where the item “phone” was found in that text.
If we instead index and print out the elements in the string between [10:15] we’re actually going to see the word phone printed out as shown below.
So that’s where it found what it matched we can now go back to printing out our match.
Suppose that we wanted to match a phone number, we could type “555-555-555”.But then we’re only ever going to match this specific string so that’s not that useful. But what Python RegEx allows us to do is it allows us to provide a pattern that will match all or any of these whatever we give it.
We are going to use “\d” before specifying the pattern. This is going to tell python that we’re going to match a digit. Now If we know how many digits we have we can just type them in several times but that’s not that useful or not that handy.
However, we can give it a plus sign so that’s going to match any amount of digits in a row.
So if we run this now, you might think that we get the whole of this string. But we don’t; we only return the very 1st 555-555-555 because it goes along the string but stops at the hyphen.
What we can do is we can actually use “\d+-\d+-\d+” which is a specific Python RegEx string that is built to match any numbers that are in this format. Now if we run this code we will obtain the match as shown in the code below.
If we make small alterations to the number we should still be able to match the existing pattern in the number as shown below.
This is just a demo on numbers, but we can go onward and match whole pieces, match case letters, and everything else. It is quite a vast subject.
Summary
This is how Python regex works in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
