📄 re-objects.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>4.2.4 Regular Expression Objects </title>
<META NAME="description" CONTENT="4.2.4 Regular Expression Objects ">
<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="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.html">
<LINK REL="previous" href="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.html">
<LINK REL="up" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">
<LINK REL="next" href="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.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="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.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="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.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="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.html">4.2.3 Module Contents</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">4.2 re </A>
<b class="navlabel">Next:</b> <a class="sectref" href="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.html">4.2.5 Match Objects</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H2>
<BR>
4.2.4 Regular Expression Objects
</H2>
<P>
Compiled regular expression objects support the following methods and
attributes:
<P>
<dl><dt><b><a name='l2h-611'><tt class='method'>search</tt></a></b> (<var>string</var><big>[</big><var>, pos</var><big>[</big><var>,
endpos</var><big>]</big><big>]</big>)
<dd>
Scan through <var>string</var> looking for a location where this regular
expression produces a match, and return a
corresponding <tt class="class">MatchObject</tt> instance. Return <code>None</code> if no
position in the string matches the pattern; note that this is
different from finding a zero-length match at some point in the string.
<P>
The optional <var>pos</var> and <var>endpos</var> parameters have the same
meaning as for the <tt class="method">match()</tt> method.
</dl>
<P>
<dl><dt><b><a name='l2h-612'><tt class='method'>match</tt></a></b> (<var>string</var><big>[</big><var>, pos</var><big>[</big><var>,
endpos</var><big>]</big><big>]</big>)
<dd>
If zero or more characters at the beginning of <var>string</var> match
this regular expression, return a corresponding
<tt class="class">MatchObject</tt> instance. Return <code>None</code> if the string does not
match the pattern; note that this is different from a zero-length
match.
<P>
<b>Note:</b> If you want to locate a match anywhere in
<var>string</var>, use <tt class="method">search()</tt> instead.
<P>
The optional second parameter <var>pos</var> gives an index in the string
where the search is to start; it defaults to <code>0</code>. This is not
completely equivalent to slicing the string; the <code>''</code> pattern
character matches at the real beginning of the string and at positions
just after a newline, but not necessarily at the index where the search
is to start.
<P>
The optional parameter <var>endpos</var> limits how far the string will
be searched; it will be as if the string is <var>endpos</var> characters
long, so only the characters from <var>pos</var> to <var>endpos</var> will be
searched for a match.
</dl>
<P>
<dl><dt><b><a name='l2h-613'><tt class='method'>split</tt></a></b> (<var>string</var><big>[</big><var>,
maxsplit<code> = 0</code></var><big>]</big>)
<dd>
Identical to the <tt class="function">split()</tt> function, using the compiled pattern.
</dl>
<P>
<dl><dt><b><a name='l2h-614'><tt class='method'>findall</tt></a></b> (<var>string</var>)
<dd>
Identical to the <tt class="function">findall()</tt> function, using the compiled pattern.
</dl>
<P>
<dl><dt><b><a name='l2h-615'><tt class='method'>sub</tt></a></b> (<var>repl, string</var><big>[</big><var>, count<code> = 0</code></var><big>]</big>)
<dd>
Identical to the <tt class="function">sub()</tt> function, using the compiled pattern.
</dl>
<P>
<dl><dt><b><a name='l2h-616'><tt class='method'>subn</tt></a></b> (<var>repl, string</var><big>[</big><var>,
count<code> = 0</code></var><big>]</big>)
<dd>
Identical to the <tt class="function">subn()</tt> function, using the compiled pattern.
</dl>
<P>
<dl><dt><b><a name='l2h-617'><tt class='member'>flags</tt></a></b>
<dd>
The flags argument used when the regex object was compiled, or
<code>0</code> if no flags were provided.
</dl>
<P>
<dl><dt><b><a name='l2h-618'><tt class='member'>groupindex</tt></a></b>
<dd>
A dictionary mapping any symbolic group names defined by
<tt class="regexp">(?P<<var>id</var>>)</tt> to group numbers. The dictionary is empty if no
symbolic groups were used in the pattern.
</dl>
<P>
<dl><dt><b><a name='l2h-619'><tt class='member'>pattern</tt></a></b>
<dd>
The pattern string from which the regex object was compiled.
</dl>
<P>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.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="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.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="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.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="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.html">4.2.3 Module Contents</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">4.2 re </A>
<b class="navlabel">Next:</b> <a class="sectref" href="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.html">4.2.5 Match Objects</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 + -