[ ![Python logo](../_static/py.svg) ](https://www.python.org/)

Theme  Auto  Light  Dark

###  [ Table of Contents ](../contents.html)

  * ` re  ` â Regular expression operations 
    * Regular Expression Syntax 
    * Module Contents 
      * Flags 
      * Functions 
      * Exceptions 
    * Regular Expression Objects 
    * Match Objects 
    * Regular Expression Examples 
      * Checking for a Pair 
      * Simulating scanf() 
      * search() vs. match() 
      * Making a Phonebook 
      * Text Munging 
      * Finding all Adverbs 
      * Finding all Adverbs and their Positions 
      * Raw String Notation 
      * Writing a Tokenizer 

####  Previous topic

[ ` string  ` â Common string operations ](string.html "previous chapter")

####  Next topic

[ ` difflib  ` â Helpers for computing deltas ](difflib.html "next chapter")

###  This Page

  * [ Report a Bug ](../bugs.html)
  * [ Show Source ](https://github.com/python/cpython/blob/main/Doc/library/re.rst)

###  Navigation

  * [ index ](../genindex.html "General Index")
  * [ modules ](../py-modindex.html "Python Module Index") | 
  * [ next ](difflib.html "difflib â Helpers for computing deltas") | 
  * [ previous ](string.html "string â Common string operations") | 
  * ![Python logo](../_static/py.svg)
  * [ Python ](https://www.python.org/) » 
  *   *   * [ 3.12.3 Documentation ](../index.html) » 
  * [ The Python Standard Library ](index.html) » 
  * [ Text Processing Services ](text.html) » 
  * [ ` re  ` â Regular expression operations ]()
  * | 
  * Theme  Auto  Light  Dark  | 

#  ` re  ` â Regular expression operations  Â¶

**Source code:** [ Lib/re/
](https://github.com/python/cpython/tree/3.12/Lib/re/)

* * *

This module provides regular expression matching operations similar to those
found in Perl.

Both patterns and strings to be searched can be Unicode strings ( [ ` str  `
](stdtypes.html#str "str") ) as well as 8-bit strings ( [ ` bytes  `
](stdtypes.html#bytes "bytes") ). However, Unicode strings and 8-bit strings
cannot be mixed: that is, you cannot match a Unicode string with a bytes
pattern or vice-versa; similarly, when asking for a substitution, the
replacement string must be of the same type as both the pattern and the search
string.

Regular expressions use the backslash character ( ` '\'  ` ) to indicate
special forms or to allow special characters to be used without invoking their
special meaning. This collides with Pythonâs usage of the same character for
the same purpose in string literals; for example, to match a literal
backslash, one might have to write ` '\\\\'  ` as the pattern string, because
the regular expression must be ` \\  ` , and each backslash must be expressed
as ` \\  ` inside a regular Python string literal. Also, please note that any
invalid escape sequences in Pythonâs usage of the backslash in string
literals now generate a [ ` SyntaxWarning  ` ](exceptions.html#SyntaxWarning
"SyntaxWarning") and in the future this will become a [ ` SyntaxError  `
](exceptions.html#SyntaxError "SyntaxError") . This behaviour will happen even
if it is a valid escape sequence for a regular expression.

The solution is to use Pythonâs raw string notation for regular expression
patterns; backslashes are not handled in any special way in a string literal
prefixed with ` 'r'  ` . So ` r"\n"  ` is a two-character string containing `
'\'  ` and ` 'n'  ` , while ` "\n"  ` is a one-character string containing a
newline. Usually patterns will be expressed in Python code using this raw
string notation.

It is important to note that most regular expression operations are available
as module-level functions and methods on  compiled regular expressions  . The
functions are shortcuts that donât require you to compile a regex object
first, but miss some fine-tuning parameters.

See also

The third-party [ regex ](https://pypi.org/project/regex/) module, which has
an API compatible with the standard library  ` re  ` module, but