The regex search() method in Python is a method used to search for a pattern in a string and return the first occurrence of the pattern. This search method is part of the Python re module.
The re is one of Python modules that is feature-rich and it comes with a lot of methods that help you to carry out your text-processing tasks without any hassle. One of the popular methods that is commonly used, is the regex search() method.
This tutorial will walk you through the regex search() method its syntax, usage, and how it compares with the other re module popular methods like the match() method. Below is the highlight of the topic that I will be covering.
The syntax of the search() method
Searching for a simple pattern
Using regular expressions to search for complex patterns
Using flags with the search() method
re.IGNORECASE
re.MULTILINE
re.DOTALL
Conclusion
1. Syntax of regex (re) search() method
To make the most of every method in the re module you should understand its ins and outs. So in this section, you will get to understand the search() method syntax. Here below is the syntax:
# Syntax
re.search(pattern, string, flags=0)
1.1 Parameters
If you look at the above syntax, you will see that the search() method takes two arguments namely pattern and string. The table below summarises the arguments:
ArgumentDescriptionpatternpattern is the regular expression pattern to search for.stringThe text to search the pattern in.flags(optional)This is for modifying the search behavior.search() method arguments
2. Use regex search() to search for a simple pattern
Now that the syntax has been taken care of, we will now switch our attention to demonstrating a real-world example of the Python regex search() method. Let us start with a simple pattern search, the code for that will look as follows:
# Import re module
import re
# Initialize text & pattern
text = “Spark By Examples”
pattern = “Spark”
# Search for pattern in a text
result = re.search(pattern, text)
# Check the result
if result:
print(f”‘{pattern}’ found in ‘{text}'”)
else:
print(f”‘{pattern}’ not found in ‘{text}'”)
In the code snippet, we have Spark By Examples as text and Spark as a pattern. The search() method will take these as arguments, and the returned result is stored in a variable result. With the help of the Python if-else the code will check if the result is True or False. If result is True, it means that the pattern was found in the string, and the code prints a message indicating that the pattern was found. If result is False, it means that the pattern was not found in the string.
The output:
# Output:
‘Spark’ found in ‘Spark By Examples’
The re.search(pattern, text) actually returns a Match object that contains information about the first occurrence of the pattern in the string. You can then use methods of the Match object, such as group(), to extract the matched string.
print(result.group()) # Output: “Spark”
Note that re.search() only returns the first occurrence of the pattern in the string. If you want to find all occurrences of the pattern, you can use the re.findall() method instead.
3. Using regular expressions to search for complex patterns
Another scenario where the python regex search() method could come in handy is when you want to search for complex patterns. On this one, you might want to search for characters that come after a certain character, etc. For this to make sense let us look at the following example:
# Example to search for complex patterns
import re
text = “SparkByExamples. One Stop For All Code Examples.”
pattern = r”All…………..”
result = re.search(pattern, text)
if result:
print(“Match found:”, result.group())
else:
print(“Match not found.”)
This code uses the re module to perform a search for the pattern r”All…………..” in the given text SparkByExamples. One Stop For All Code Examples. The pattern contains 14 dots that represent any character. Hence, the pattern will match any string of length 17 that starts with All and ends with any 3 characters.
The search() method is used to search for the given pattern in the text. If the pattern is found, the group() method will return the matched string.
Output:
# Output:
Match found: All Code Examples
4. Using flags with the regex search() method
In scenarios where you want to modify the behavior of your search, flags are the go-to solution. flags are an optional argument for the regex search() method in Python, this means that you can do a search with or without them. Some of these flags for the search() method are:
FlagDescriptionre.IGNORECASEUsed to perform a case-insensitive searchre.MULTILINEUsed to search for patterns in multiple linesre.DOTALLUsed to make the . character match any character, including newline charactersre.UNICODEThis enable Unicode matchingsearch() method flags
4.1 re.IGNORECASE
Let us look at an example of how we can use the re.IGNORECASE flag:
# Using re.IGNORECASE
import re
text = “SparkByExamples. One Stop For All CODE EXAMPLES.”
pattern = r”code examples”
result = re.search(pattern, text, re.IGNORECASE)
if result:
print(f”Match found: ‘{result.group()}'”)
else:
print(“Match not found.”)
In the code block, the re.IGNORECASE makes the search case insensitive. This means that the pattern code examples will match CODE EXAMPLES in the text string.
Output:
# Output:
Match found: ‘CODE EXAMPLES’
4.2 re.MULTILINE
Here is the code to demonstrate the re.MULTILINE flag with the search() method:
# Using re.MULTILINE
import re
text = “This is SparkByExamplesnThis is SparkByExamplesnThis is SparkByExamples”
pattern = r”^This”
result = re.findall(pattern, text, re.MULTILINE)
print(result)
The re.MULTILINE flag is used to ensure that the pattern matches the beginning of each line instead of just the beginning of the string. The result of the findall() method is a list of all the matches found in the text. In this case, the result is a list containing three elements, since This appears at the beginning of each line in the text.
Output:
# Output:
[‘This’, ‘This’, ‘This’]
4.3 re.DOTALL
The code where re.DOTALL has been used could look like this:
# Using re.DOTALL
import re
text = “SparkByExamples.nOne Stop For All Code Examples.”
pattern = r”Spark.*Examples”
result = re.search(pattern, text, re.DOTALL)
if result:
print(“Match found:”, result.group())
else:
print(“Match not found.”)
The code snippet above searches for the pattern Spark.*Examples in the text SparkByExamples.nOne Stop For All Code Examples. with the help of the re.search() method. The pattern includes .*, which matches any character except a newline character.
The re.DOTALL flag is used to include newline characters in the text. Therefore, the search will match the entire string SparkByExamples.nOne Stop For All Code Examples since it contains Spark and Examples separated by any characters including a newline character.
Output:
# Output:
Match found: SparkByExamples.
One Stop For All Code Examples
5. Conclusion
That’s it from this tutorial, we hope you now have full knowledge of the search() method in the re module. The best way to learn code is to code, so I advise you to experiment with everything you have learned in this tutorial.
The re.search() is a regex method provided by the Python re module that allows you to search a string for a pattern and return the first occurrence of the pattern.
The regex search() method in Python is a method used to search for a pattern in a string and return the first occurrence of the pattern. This search method is part of the Python re module. The re is one of Python modules that is feature-rich and it comes with a lot of methods that Read More Python, Python Tutorial