Regular expressions (regex) are a powerful tool for pattern matching and manipulation of strings in Python. The re
module in Python provides various functions to work with regular expressions, and one of them is re.fullmatch()
. In this tutorial, we will explore the re.fullmatch()
function in detail, with explanations and examples to illustrate its usage.
Table of Contents
- Introduction to
re.fullmatch()
- Syntax of
re.fullmatch()
- Parameters of
re.fullmatch()
- Return Value of
re.fullmatch()
- Examples of Using
re.fullmatch()
- Example 1: Matching a Simple Pattern
- Example 2: Matching Email Addresses
- Conclusion
1. Introduction to re.fullmatch()
The re.fullmatch()
function in the Python re
module is used to determine if the entire input string matches a given regular expression pattern. Unlike some other re
functions, such as re.search()
and re.match()
, which look for a match anywhere in the string, re.fullmatch()
requires the entire string to match the pattern from start to finish. This function is useful when you want to validate whether a string conforms to a specific pattern entirely.
2. Syntax of re.fullmatch()
The syntax of the re.fullmatch()
function is as follows:
re.fullmatch(pattern, string, flags=0)
Here,
pattern
: The regular expression pattern to match against.string
: The input string to be matched.flags
: Optional flags that modify the behavior of the regular expression (same as in otherre
functions).
3. Parameters of re.fullmatch()
pattern
: This parameter specifies the regular expression pattern you want to match against the input string. Regular expression patterns are used to define the rules for string matching. They can include various metacharacters and quantifiers to specify the structure of the desired pattern.string
: This parameter is the input string that you want to check for a match against the given pattern.flags
(optional): Flags can be used to modify how the regular expression is processed. Some commonly used flags include:re.IGNORECASE
(orre.I
): Perform a case-insensitive matching.re.MULTILINE
(orre.M
): Allow the^
and$
anchors to match at the start and end of each line, in addition to the start and end of the entire string.re.DOTALL
(orre.S
): Make the.
metacharacter match any character, including newline (\n
).re.UNICODE
(orre.U
): Enable Unicode matching.
4. Return Value of re.fullmatch()
The re.fullmatch()
function returns a match object if the entire input string matches the specified pattern. If there is no match, it returns None
.
The match object contains information about the matched string, such as the matched text and the positions of the match within the input string. You can use methods and attributes of the match object to extract this information.
5. Examples of Using re.fullmatch()
In this section, we will walk through two examples to demonstrate how to use the re.fullmatch()
function effectively.
Example 1: Matching a Simple Pattern
Let’s start with a simple example. Suppose we want to check if a given string consists of exactly three digits. We can use the following regular expression to achieve this:
import re
pattern = r'\d{3}' # Match exactly three digits
# Test strings
test_strings = ["123", "456", "7890", "abc"]
for string in test_strings:
match = re.fullmatch(pattern, string)
if match:
print(f"String '{string}' matches the pattern.")
else:
print(f"String '{string}' does not match the pattern.")
Output:
String '123' matches the pattern.
String '456' matches the pattern.
String '7890' does not match the pattern.
String 'abc' does not match the pattern.
In this example, the pattern \d{3}
matches exactly three digits. The first two test strings ("123"
and "456"
) match the pattern, while the last two test strings ("7890"
and "abc"
) do not.
Example 2: Matching Email Addresses
Now, let’s take a more complex example and use re.fullmatch()
to validate email addresses based on a specific pattern.
import re
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
# Test email addresses
test_emails = ["user@example.com", "john.doe@gmail.com", "invalid.email", "123@site.net"]
for email in test_emails:
match = re.fullmatch(pattern, email)
if match:
print(f"Email '{email}' is valid.")
else:
print(f"Email '{email}' is not valid.")
Output:
Email 'user@example.com' is valid.
Email 'john.doe@gmail.com' is valid.
Email 'invalid.email' is not valid.
Email '123@site.net' is not valid.
In this example, the pattern ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
matches valid email addresses. The ^
and $
anchors ensure that the entire string conforms to the pattern. The first two test email addresses are valid, while the last two are not.
6. Conclusion
In this tutorial, we explored the re.fullmatch()
function in the Python re
module, which is used to determine if an entire input string matches a given regular expression pattern. We discussed its syntax, parameters, and return value. We also provided two examples to illustrate how to use re.fullmatch()
for matching simple and more complex patterns, such as digits and email addresses.
Regular expressions are a powerful tool for string manipulation and pattern matching, and re.fullmatch()
adds to the arsenal of functions available in the re
module. By understanding and utilizing this function effectively, you can perform accurate and efficient string validation and manipulation tasks in your Python programs.