There are specific signs that are used in system engineering, networking and programming. With these regular expressions, some duties are done with specific signs instead of writing long lines. For example, in Linux there are some Regular Expressions. There are also regular expressions used with Routing protocol BGP in networking. In Python Programming, there are also Regular Expressions that are called RegEx. In this lesson, we will learn the details of Python RegEx.
Basically, a Python RegEx is a sequence of characters that has a specific meaning in python programming. There are different Python RegEx statements. To use them we need to import a Python Module. This python regEx module is re module.
Test yourself with Python Quizes!
Table of Contents
There are different Python RegEx functions reside in re module. We can define them in four categories. These are:
search function checks a string for any match.
findall function checks for matchings and returns with a list.
split function returns with a list where the string split for each match.
sub function replaces matches in the string
Below, we will focus on the details of these functions with examples. But firstly, let’s learn also Metacharacters, sets, special characters used with Python RegEx.
You can also watch the video of this lesson!
search() function is used to check any characters in a string and if it finds it returns the index of the first find characters.
The output of this code will be:
findall() function is used to check if the whole given characters resides in the given string. If it finds, it returns with the searched characters every time it finds it.
split() function returns with a list created with the items that are stripted with a specified character.
Below, we can create a list with the words of a sentences by using space character as split point.
The return of this code is like below:
To split the given string from any specified split point, we can use an extra parameter. Below, we will split the string from the second found space character.
The output will be:
sub() function is used to replace the given characters with the find characters.
Below, we will find space characters and we will change space character with dash(-) character.
The output will be:
Again, we can use a second parameter to show the places that we will do this change.
Beside functions, there are metacharacters used with Python RegEx that has specific meanings. So what are these metacharacters used with Python RegEx? These are given below:
Sets used with Python RegEx are the specific statements in square brackets ( [] ). The meaning of these square brackets are different according to the used characters in these square brackets. So, wghat re these set statements used with RegEx. Below, you can find some of them and their meaning:
[xyz] Returns with the characters that match any of these characters (x,y,z).
This can be also done with the numbers like [135]. Here, we can check 1,3,5 in the message.
In the below, example, we will check “l” and “y” characters in the message and the code will return with the characters that watch any of these characters.
The return of this code will be:
[a-x] Returns with the character that matches any character between a and x alphabetically.
This can be also done with the numbers like [1-5]. Here, we can check if are there any line includes these digits.
Below, we will find if are there any character between “a” and “f” in the message.
The output of this code will be:
[xyz] Returns with the characters other that any of these characters (x,y,z).
Below, we will check the message and return with the characters different than the specified characters.
The output will be like below:
[0-9][0-9] Returns with ant two-digit number match between 00 and 99.
We can do this with three or more digits like [0-9][0-9][0-9] or [0-9][0-9][0-9][0-9] etc.
Below, we will check an address and return with the two digit numbers if are there any in it.
The output will be:
[a-zA-Z] Returns with any match from a to z both lower and upper cases in a string.
The output is
Special sequences are used with the help of “/” character. After this sign, a specific lower or upper case letters is used. Below, you can find the specific sequences and examples for them.
\A It is used to check if are there any specific characters reside at the beginning of a string.
\b It is used to check if are there any specific character reside at the beginning or at the end of the string.
\B It is used to check if are there any specific character reside but not at the beginning or at the end of the string.
\d It is used to check if string has any digits.
\D It is used to get others than digits in a string.
\s It is used to get the white spaces in a string.
\S It is used to get others than white spaces in a string.
\w It is used to get word characters in a string.
\W It is used to get others than word characters in a string.
\Z It is used to check if specific characters are at the end of a string.
In this lesson, we have learned specific usages in Python programming. In other words, we have done practice on Python RegEx Statements. As we have discussed above, Python RegEx usage can be done with the help of different functions, special characters, sets and metacharacters. You can improve your Python Regular Expression skills with using these expressions more in your codes.
Leave a Reply