matching-searching.html

来自「一本很好的python的说明书,适合对python感兴趣的人」· HTML 代码 · 共 97 行

HTML
97
字号
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>4.2.2 Matching vs. Searching </title>
<META NAME="description" CONTENT="4.2.2 Matching vs. Searching ">
<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="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.html">
<LINK REL="previous" href="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.html">
<LINK REL="up" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">
<LINK REL="next" href="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.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="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.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="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.html">4.2.1 Regular Expression Syntax</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="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.html">4.2.3 Module Contents</A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H2>
<BR>
4.2.2 Matching vs. Searching 
</H2>

<P>
Python offers two different primitive operations based on regular
expressions: match and search.  If you are accustomed to Perl's
semantics, the search operation is what you're looking for.  See the
<tt class="function">search()</tt> function and corresponding method of compiled
regular expression objects.

<P>
Note that match may differ from search using a regular expression
beginning with "<tt class="character">^</tt>": "<tt class="character">^</tt>" matches only at the
start of the string, or in <tt class="constant">MULTILINE</tt> mode also immediately
following a newline.  The ``match'' operation succeeds only if the
pattern matches at the start of the string regardless of mode, or at
the starting position given by the optional <var>pos</var> argument
regardless of whether a newline precedes it.

<P>
<dl><dd><pre class="verbatim">
re.compile("a").match("ba", 1)           # succeeds
re.compile("^a").search("ba", 1)         # fails; 'a' not at start
re.compile("^a").search("\na", 1)        # fails; 'a' not at start
re.compile("^a", re.M).search("\na", 1)  # succeeds
re.compile("^a", re.M).search("ba", 1)   # fails; no preceding \n
</pre></dl>

<P>

<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.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="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.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="re-syntax.html" tppabs="http://www.python.org/doc/current/lib/re-syntax.html">4.2.1 Regular Expression Syntax</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="Contents_of_Module_re.html" tppabs="http://www.python.org/doc/current/lib/Contents_of_Module_re.html">4.2.3 Module Contents</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 + =
减小字号Ctrl + -
显示快捷键?