Regular expressions are powerful tools for finding and manipulating text patterns in Python. The “re” module contains a number of useful functions. One of them is the “re.compile()” function which allows us to compile a regular expression pattern into a reusable object that can be applied to different strings.
This post provides an in-depth guide on how to compile a regular expression pattern in string format with the help of the Python “re.compile()” method. This post will cover the following topics:
Python re.compile() Method
In Python, the “re.compile()” is utilized to compile/assemble an input pattern into a regex pattern object. The regex pattern object is useful for matching, searching, replacing, etc. The syntax of the “re.compile()” method is shown below:
re.compile(pattern, flags=0)
In the above syntax, the “pattern” parameter is a string containing the regex pattern, and the “flags” parameter is optional modifiers that affect the interpretation of the pattern.
Example 1: Finding Consecutive Digits From the String
The below code is used to find the consecutive digits from the given string:
Code:
import re
string_value = "Students Marks are 100, 250, 40, 1, 500"
re_pattern = re.compile(r"\d{3}")
print(re_pattern.findall(string_value))
- The module named “re” is imported.
- The “re.compile()” function takes a string pattern “\d{3}” and compiles it into a regex pattern object. The regular expression r”\d{3}” means three digits in a row.
- The “re.findall()” method is utilized to find/get all the matched strings from the given string based on the particular pattern.
Output:
The matched strings have been returned successfully.
Example 2: Finding Specific Patterns From the String
The following code is used to get the particular string from the given string using a specific pattern:
Code:
import re
string_value = "Python Guide Provided by ITSLINUXFOSS"
re_pattern = re.compile(r"(\b[A-Z]+\b)")
print(re_pattern.findall(string_value))
- The “re.compile()” function takes the pattern as an argument and creates a regex pattern object.
- The “re.findall()” function returns the list of matched strings from the given string.
Output:
The matched string pattern has been displayed.
Example 3: Finding all Digits From the String
The following code is used to find all digits from the specified strings:
Code:
import re
string_value = "I'll leave for the airport at 3 p.m. on Feb 23, 2022"
re_pattern = re.compile('\d')
print(re_pattern.findall(string_value))
- The module named “re” is imported and the string is initialized at the start.
- The “re.compile()” takes the pattern “\d” and compiles it into a regex pattern. This pattern means any digit.
- The “re.findall()” method is used to find all matches of the re-pattern from the string.
Output:
The digits found in the strings have returned as a list.
Conclusion
The “re.compile()” function of the re-module is utilized to compile/collect a regex pattern into a regex pattern object in Python. The specified string pattern is given as an argument to the “re.compile()” function for finding consecutive digits, finding specified string patterns, finding all digits, etc. from the given string. This tutorial presented a detailed guide on compiling patterns using the “re.compile()” function.