<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>4.2 re --
Perl-style regular expression operations.</TITLE>
<META NAME="description" CONTENT="4.2 re --
Perl-style regular expression operations.">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="module-regex.html">
<LINK REL="previous" HREF="module-string.html">
<LINK REL="up" HREF="strings.html">
<LINK REL="next" HREF="re-syntax.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="re-syntax.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="strings.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-string.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="re-syntax.html">4.2.1 Regular Expression Syntax</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="strings.html">4. String Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-string.html">4.1 string  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION006200000000000000000">
4.2 <tt class=module>re</tt> --
         Perl-style regular expression operations.</A>
</H1>


<P>

<P>
This module provides regular expression matching operations similar to
those found in Perl.  It's 8-bit clean: the strings being processed
may contain both null bytes and characters whose high bit is set.  Regular
expression pattern strings may not contain null bytes, but can specify
the null byte using the <tt>&#92;<var>number</var></tt> notation.
Characters with the high bit set may be included.  The <tt class=module>re</tt>
module is always available.

<P>
Regular expressions use the backslash character ("<tt class=character>&#92;</tt>") 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
<tt>'&#92;&#92;&#92;&#92;'</tt> as the pattern string, because the regular expression
must be "<tt class=samp>&#92;&#92;</tt>", and each backslash must be expressed as
"<tt class=samp>&#92;&#92;</tt>" inside a regular Python string literal. 

<P>
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 "<tt class=character>r</tt>".  So <tt>r"&#92;n"</tt> is a
two-character string containing "<tt class=character>&#92;</tt>" and "<tt class=character>n</tt>",
while <tt>"&#92;n"</tt> is a one-character string containing a newline.
Usually patterns will be expressed in Python code using this raw
string notation.

<P>

<p><hr>
<!--Table of Child-Links-->


<UL>
<LI><A NAME="tex2html1483"
 HREF="re-syntax.html">4.2.1 Regular Expression Syntax </A>
<LI><A NAME="tex2html1484"
 HREF="matching-searching.html">4.2.2 Matching vs. Searching </A>
<LI><A NAME="tex2html1485"
 HREF="Contents_of_Module_re.html">4.2.3 Module Contents</A>
<LI><A NAME="tex2html1486"
 HREF="re-objects.html">4.2.4 Regular Expression Objects </A>
<LI><A NAME="tex2html1487"
 HREF="match-objects.html">4.2.5 Match Objects </A>
</UL>
<!--End of Table of Child-Links-->
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="re-syntax.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="strings.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-string.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="re-syntax.html">4.2.1 Regular Expression Syntax</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="strings.html">4. String Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-string.html">4.1 string  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
