GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python re.fullmatch - Mastering Full Pattern Matching Python re.fullmatch() Functionlast modified April 20, 2025 Introduction to re.fullmatch
The
Unlike
The function returns a match object if the pattern matches the whole
string, or Basic Syntax
The syntax for re.fullmatch(pattern, string, flags=0)
The Basic Full Match ExampleLet's start with a simple example of validating a string format. basic_fullmatch.py
#!/usr/bin/python
import re
pattern = r'hello'
text1 = "hello"
text2 = "hello world"
match1 = re.fullmatch(pattern, text1)
match2 = re.fullmatch(pattern, text2)
print(f"Text1 match: {match1 is not None}")
print(f"Text2 match: {match2 is not None}")
This example shows how match1 = re.fullmatch(pattern, text1) This returns a match object because "hello" exactly matches the pattern. The entire string must conform to the pattern. match2 = re.fullmatch(pattern, text2)
This returns Validating Email Format
email_validation.py
#!/usr/bin/python
import re
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
emails = ["[email protected]", "invalid.email", "[email protected]"]
for email in emails:
if re.fullmatch(email_pattern, email):
print(f"Valid email: {email}")
else:
print(f"Invalid email: {email}")
This demonstrates using Matching Whole NumbersHere's how to validate that a string contains only digits. number_validation.py
#!/usr/bin/python
import re
numbers = ["123", "45.67", "1000", "abc123"]
pattern = r'^\d+$'
for num in numbers:
if re.fullmatch(pattern, num):
print(f"Valid number: {num}")
else:
print(f"Invalid number: {num}")
The pattern Using with Compiled PatternsFor better performance with repeated use, compile the pattern first. compiled_pattern.py
#!/usr/bin/python
import re
date_pattern = re.compile(r'^\d{4}-\d{2}-\d{2}$')
dates = ["2023-12-25", "12/25/2023", "2023-13-01"]
for date in dates:
if date_pattern.fullmatch(date):
print(f"Valid date: {date}")
else:
print(f"Invalid date: {date}")
Compiled patterns have a Password Strength CheckerCombine multiple conditions to validate password strength. password_checker.py
#!/usr/bin/python
import re
password_pattern = r'^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$'
passwords = ["Weak1", "Strong@123", "noSpecialChar1"]
for pwd in passwords:
if re.fullmatch(password_pattern, pwd):
print(f"Strong password: {pwd}")
else:
print(f"Weak password: {pwd}")
This pattern requires at least one uppercase, lowercase, digit, and
special character, with minimum length of 8. Hex Color Code Validation
Validate CSS-style hex color codes with color_validation.py
#!/usr/bin/python
import re
color_pattern = r'^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$'
colors = ["#FF5733", "#abc", "#123456", "#ghijkl"]
for color in colors:
if re.fullmatch(color_pattern, color):
print(f"Valid color: {color}")
else:
print(f"Invalid color: {color}")
The pattern matches both 3-digit and 6-digit hex color codes. The
Time Format ValidationValidate 24-hour time format with optional seconds. time_validation.py
#!/usr/bin/python
import re
time_pattern = r'^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$'
times = ["13:45", "23:59:59", "25:00", "12:60"]
for time in times:
if re.fullmatch(time_pattern, time):
print(f"Valid time: {time}")
else:
print(f"Invalid time: {time}")
This pattern validates hours (00-23), minutes (00-59), and optional
seconds (00-59). Best Practices
When using
Performance Considerations
When validating many strings against the same pattern, compiling the pattern first provides better performance. The difference is most noticeable in tight loops. SourcePython re.fullmatch() documentation
This tutorial covered the essential aspects of Python's AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |