Content · Programming · Python

Python Regex Search vs Prefix Match vs Full Match

Choose search, prefix matching, or full matching based on your goal.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Choose search, prefix matching, or full matching based on your goal.

How It Works


re.search() finds a pattern anywhere; re.prefixmatch() requires the start; re.fullmatch() requires all characters. Beginning-only checks are not equivalent to complete validation. The prefixmatch name was introduced in Python.

Step-by-Step Example


import re
value = 'AB-12 extra'
pattern = r'AB-\d+'
print(bool(re.search(pattern, value)))
print(bool(re.prefixmatch(pattern, value)))
print(bool(re.fullmatch(pattern, value)))

Understanding the Example


  • The expression "'AB-12 extra'" supplies the value stored as 'value'. This value is then used or checked by the later statements.
  • The expression "r'AB-\\d+'" supplies the value stored as 'pattern'. This value is then used or checked by the later statements.

This example requires a newer interpreter or an optional standard-library component. If the import or function is unavailable, check the installed version and distribution before running it.

Where This Fits in a Real Program


Start by writing down examples that should match and examples that must fail. Check whether you need to locate a substring, match a prefix or validate an entire field; these are different operations. Unicode and escape sequences can also change which characters are matched, so test accented text and empty input.

Practice: Try It Yourself


Earlier Python releases expose the same beginning-only behavior as re.match().

For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.

Further Reading


Read the official Python documentation for complete parameters, return types and exceptions.

Common Beginner Mistakes


  • Using a prefix match when trailing extra input must be rejected, or mixing start anchors with MULTILINE assumptions.
  • Compare the three matching operations for valid and invalid IDs.

Key Takeaways


  • Choose search, prefix matching, or full matching based on your goal.
  • re.search() finds a pattern anywhere; re.prefixmatch() requires the start; re.fullmatch() requires all characters.
  • Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.
Share this resourceSend this page to someone who may find it useful.