Regular Expressions in Python
Regular Expressions in Python
Regular expressions are defined as a combination of characters which are used to search for a pattern in a string. We use regular expressions in many ways. But here we will know the basics and how they work.
Searching enables us to perform a wide range of activities. Some such features include:
- Recognizing the tone of a person from an email.
- Analyzing the mood of people from text chats.
- Semantic analysis of human speech.
In all these cases, we use the concept of a regular expression to some extent. In python, we can use it by using the re module. But the same module also throws an error whenever there is a syntactical error in the program.
In this article, we will stick to a few basic functionalities of Regular expression. Few important functions include:
findall() | To find all the occurrence of searched character in the string |
---|---|
split() | contains string which has been split to find the match during search |
sub() | substitutes all the occurrences of that character with the required character. |
match | searches for match and returns true or false accordingly. |
search() | search is embedded inside match object and returns the value of match accordingly |
Let’s take a few of these examples and see how they work.
findall() function
We use this function to find all the occurrences of the sample character. It generates all the matching sequences in a list format. This code will make you understand this much better:
import re str = "My name is Mario. I love to eat Mushrooms. I grow big because of these Mushrooms." print(str) match = re.findall("Mushrooms", str) print(type(match)) print(match)
In the first print statement, we are determining the type of output. It will show us as a list. In second print it will show us the actual list containing the number of times the word is occurring in the given string.
sub() function
We use this function to find the repeated occurrence and replace it with the desired character which will be given as a parameter. The following code will make this clear:
import re str = "My name is Mario. I love to eat Mushrooms. I grow big because of these Mushrooms." print(str) subs = re.sub("Mushrooms", "Coins", str) print(subs)
In this case, we replace the word Mushrooms
with the word Coins
. However, the new string is stored in subs
variable.
match() function
We use this function to search for the match present inside the function. If present, it returns the span()
value and match object. If not present, then it returns none. Run the following code to get a clear overview.
import re str = "My name is Mario. I love to eat Mushrooms. I grow big because of these Mushrooms." print(str) sear = re.search("Mushrooms", str) print(sear)
Here the search will be successful since the term Mushroom
is present in the string. Try and replace it with any other term. It will give the output as None
.
These were some of the basic terms and functions in regular expression in Python. For more advanced concepts we need to know about python databases and file handling in python.
No comment yet, add your voice below!