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.

 

Python

 

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.

 

Learn Python RegEx online

 

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.

 

In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

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.