📄 module-re.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>4.2 re -- Regular expression operations</title>
<META NAME="description" CONTENT="4.2 re -- 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" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-struct.html" tppabs="http://www.python.org/doc/current/lib/module-struct.html">
<LINK REL="previous" href="module-string.html" tppabs="http://www.python.org/doc/current/lib/module-string.html">
<LINK REL="up" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">
<LINK REL="next" href="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-string.html" tppabs="http://www.python.org/doc/current/lib/module-string.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-string.html" tppabs="http://www.python.org/doc/current/lib/module-string.html">4.1 string </A>
<b class="navlabel">Up:</b> <a class="sectref" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">4. String Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.html">4.2.1 Regular Expression Syntax</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION006200000000000000000">
4.2 <tt class="module">re</tt> --
Regular expression operations</A>
</H1>
<P>
<P>
This module provides regular expression matching operations similar to
those found in Perl. Regular expression pattern strings may not
contain null bytes, but can specify the null byte using the
<code>\<var>number</var></code> notation. Both patterns and strings to be
searched can be Unicode strings as well as 8-bit strings. The
<tt class="module">re</tt> module is always available.
<P>
Regular expressions use the backslash character ("<tt class="character">\</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
<code>'\\\\'</code> as the pattern string, because the regular expression
must be "<tt class="samp">\\</tt>", and each backslash must be expressed as
"<tt class="samp">\\</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 <code>r"\n"</code> is a
two-character string containing "<tt class="character">\</tt>" and "<tt class="character">n</tt>",
while <code>"\n"</code> is a one-character string containing a newline.
Usually patterns will be expressed in Python code using this raw
string notation.
<P>
<b>Implementation note:</b>
The <tt class="module">re</tt> module has two distinct
implementations: <tt class="module">sre</tt> is the default implementation and
includes Unicode support, but may run into stack limitations for some
patterns. Though this will be fixed for a future release of Python,
the older implementation (without Unicode support) is still available
as the <tt class="module">pre</tt> module.
<P>
<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html1743"
href="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.html">4.2.1 Regular Expression Syntax </A>
<LI><A NAME="tex2html1744"
href="matching-searching.html" tppabs="http://www.python.org/doc/current/lib/matching-searching.html">4.2.2 Matching vs. Searching </A>
<LI><A NAME="tex2html1745"
href="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.html">4.2.3 Module Contents</A>
<LI><A NAME="tex2html1746"
href="re-objects.html" tppabs="http://www.python.org/doc/current/lib/re-objects.html">4.2.4 Regular Expression Objects </A>
<LI><A NAME="tex2html1747"
href="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.html">4.2.5 Match Objects </A>
</UL>
<!--End of Table of Child-Links-->
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-string.html" tppabs="http://www.python.org/doc/current/lib/module-string.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-string.html" tppabs="http://www.python.org/doc/current/lib/module-string.html">4.1 string </A>
<b class="navlabel">Up:</b> <a class="sectref" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">4. String Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.html">4.2.1 Regular Expression Syntax</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -