📄 contents_of_module_re.html
字号:
the regular expression <var>pattern</var>, 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.
</dl>
<P>
<dl><dt><b><a name='l2h-599'><tt class='function'>split</tt></a></b> (<var>pattern, string</var><big>[</big><var>, maxsplit<code> = 0</code></var><big>]</big>)
<dd>
Split <var>string</var> by the occurrences of <var>pattern</var>. If
capturing parentheses are used in <var>pattern</var>, then the text of all
groups in the pattern are also returned as part of the resulting list.
If <var>maxsplit</var> is nonzero, at most <var>maxsplit</var> splits
occur, and the remainder of the string is returned as the final
element of the list. (Incompatibility note: in the original Python
1.5 release, <var>maxsplit</var> was ignored. This has been fixed in
later releases.)
<P>
<dl><dd><pre class="verbatim">
>>> re.split('\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split('(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>> re.split('\W+', 'Words, words, words.', 1)
['Words', 'words, words.']
</pre></dl>
<P>
This function combines and extends the functionality of
the old <tt class="function">regsub.split()</tt> and <tt class="function">regsub.splitx()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-600'><tt class='function'>findall</tt></a></b> (<var>pattern, string</var>)
<dd>
Return a list of all non-overlapping matches of <var>pattern</var> in
<var>string</var>. If one or more groups are present in the pattern,
return a list of groups; this will be a list of tuples if the pattern
has more than one group. Empty matches are included in the result.
New in version 1.5.2.
</dl>
<P>
<dl><dt><b><a name='l2h-601'><tt class='function'>sub</tt></a></b> (<var>pattern, repl, string</var><big>[</big><var>, count<code> = 0</code></var><big>]</big>)
<dd>
Return the string obtained by replacing the leftmost non-overlapping
occurrences of <var>pattern</var> in <var>string</var> by the replacement
<var>repl</var>. If the pattern isn't found, <var>string</var> is returned
unchanged. <var>repl</var> can be a string or a function; if a function,
it is called for every non-overlapping occurrence of <var>pattern</var>.
The function takes a single match object argument, and returns the
replacement string. For example:
<P>
<dl><dd><pre class="verbatim">
>>> def dashrepl(matchobj):
.... if matchobj.group(0) == '-': return ' '
.... else: return '-'
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
'pro--gram files'
</pre></dl>
<P>
The pattern may be a string or a
regex object; if you need to specify
regular expression flags, you must use a regex object, or use
embedded modifiers in a pattern; e.g.
"<tt class="samp">sub("(?i)b+", "x", "bbbb BBBB")</tt>" returns <code>'x x'</code>.
<P>
The optional argument <var>count</var> is the maximum number of pattern
occurrences to be replaced; <var>count</var> must be a non-negative integer, and
the default value of 0 means to replace all occurrences.
<P>
Empty matches for the pattern are replaced only when not adjacent to a
previous match, so "<tt class="samp">sub('x*', '-', 'abc')</tt>" returns <code>'-a-b-c-'</code>.
<P>
If <var>repl</var> is a string, any backslash escapes in it are processed.
That is, "<tt class="samp">\n</tt>" is converted to a single newline character,
"<tt class="samp">\r</tt>" is converted to a linefeed, and so forth. Unknown escapes
such as "<tt class="samp">\j</tt>" are left alone. Backreferences, such as "<tt class="samp">\6</tt>", are
replaced with the substring matched by group 6 in the pattern.
<P>
In addition to character escapes and backreferences as described
above, "<tt class="samp">\g<name></tt>" will use the substring matched by the group
named "<tt class="samp">name</tt>", as defined by the <tt class="regexp">(?P<name>...)</tt> syntax.
"<tt class="samp">\g<number></tt>" uses the corresponding group number; "<tt class="samp">\
g<2></tt>" is therefore equivalent to "<tt class="samp">\2</tt>", but isn't ambiguous in a
replacement such as "<tt class="samp">\g<2>0</tt>". "<tt class="samp">\20</tt>" would be
interpreted as a reference to group 20, not a reference to group 2
followed by the literal character "<tt class="character">0</tt>".
</dl>
<P>
<dl><dt><b><a name='l2h-602'><tt class='function'>subn</tt></a></b> (<var>pattern, repl, string</var><big>[</big><var>, count<code> = 0</code></var><big>]</big>)
<dd>
Perform the same operation as <tt class="function">sub()</tt>, but return a tuple
<code>(<var>new_string</var>, <var>number_of_subs_made</var>)</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-603'><tt class='function'>escape</tt></a></b> (<var>string</var>)
<dd>
Return <var>string</var> with all non-alphanumerics backslashed; this is
useful if you want to match an arbitrary literal string that may have
regular expression metacharacters in it.
</dl>
<P>
<dl><dt><b><a name='l2h-604'><tt class='exception'>error</tt></a></b>
<dd>
Exception raised when a string passed to one of the functions here
is not a valid regular expression (e.g., unmatched parentheses) or
when some other error occurs during compilation or matching. It is
never an error if a string contains no match for a pattern.
</dl>
<P>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="matching-searching.html" tppabs="http://www.python.org/doc/current/lib/matching-searching.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="re-objects.html" tppabs="http://www.python.org/doc/current/lib/re-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="matching-searching.html" tppabs="http://www.python.org/doc/current/lib/matching-searching.html">4.2.2 Matching vs. Searching</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="re-objects.html" tppabs="http://www.python.org/doc/current/lib/re-objects.html">4.2.4 Regular Expression 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 + -