VOOZH about

URL: https://www.geeksforgeeks.org/python/python-re-compile/

⇱ Python - re.compile() - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - re.compile()

Last Updated : 23 Jul, 2025

In Python, re.compile() from the re module creates a regex object from a regular expression pattern. This object lets you perform operations like search(), match(), and findall(). In this article, we will understand about re.compile() method.


Output
3

Syntax of re.compile()

re.compile(pattern, flags=0)

Parameters:

  1. pattern (str):
    • The regular expression pattern you want to compile.
    • Example: r'\d+' (matches one or more digits).
  2. flags (optional):
    • Modifiers that change the behavior of the regex.
    • Common flags:
      • re.IGNORECASE or re.I: Ignore case sensitivity.
      • re.MULTILINE or re.M: Allow ^ and $ to match start and end of lines.
      • re.DOTALL or re.S: Allow . to match newline characters.
      • re.VERBOSE or re.X: Allow adding comments and whitespace for better readability.

Without using flags

In this code we are not using flags which is an optional parameter:


Output
Found: 123-45-6789

With Using Flags

In this code we are using flags (Modifiers that change the behavior of the regex.)


Output
 Hello
Comment
Article Tags: