⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 re-syntax.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>4.2.1 Regular Expression Syntax </title>
<META NAME="description" CONTENT="4.2.1 Regular Expression Syntax ">
<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="matching-searching.html" tppabs="http://www.python.org/doc/current/lib/matching-searching.html">
<LINK REL="previous" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/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="matching-searching.html" tppabs="http://www.python.org/doc/current/lib/matching-searching.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-re.html" tppabs="http://www.python.org/doc/current/lib/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="matching-searching.html" tppabs="http://www.python.org/doc/current/lib/matching-searching.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-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">4.2 re  </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="matching-searching.html" tppabs="http://www.python.org/doc/current/lib/matching-searching.html">4.2.2 Matching vs. Searching</A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H2>
<BR>
4.2.1 Regular Expression Syntax 
</H2>

<P>
A regular expression (or RE) specifies a set of strings that matches
it; the functions in this module let you check if a particular string
matches a given regular expression (or if a given regular expression
matches a particular string, which comes down to the same thing).

<P>
Regular expressions can be concatenated to form new regular
expressions; if <i>A</i> and <i>B</i> are both regular expressions,
then <i>AB</i> is also an regular expression.  If a string <i>p</i>
matches A and another string <i>q</i> matches B, the string <i>pq</i>
will match AB.  Thus, complex expressions can easily be constructed
from simpler primitive expressions like the ones described here.  For
details of the theory and implementation of regular expressions,
consult the Friedl book referenced below, or almost any textbook about
compiler construction.

<P>
A brief explanation of the format of regular expressions follows.  For
further information and a gentler presentation, consult the Regular
Expression HOWTO, accessible from <a class="url" href="javascript:if(confirm('http://www.python.org/doc/howto/  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.python.org/doc/howto/'" tppabs="http://www.python.org/doc/howto/">http://www.python.org/doc/howto/ <img src="offsite.gif" tppabs="http://www.python.org/doc/current/icons/offsite.gif"
  border='0' class='offsitelink' height='15' width='17' alt='[off-site link]'
  ></a>.

<P>
Regular expressions can contain both special and ordinary characters.
Most ordinary characters, like "<tt class="character">A</tt>", "<tt class="character">a</tt>", or "<tt class="character">0</tt>",
are the simplest regular expressions; they simply match themselves.  
You can concatenate ordinary characters, so <tt class="regexp">last</tt> matches the
string <code>'last'</code>.  (In the rest of this section, we'll write RE's in
<tt class="regexp">this special style</tt>, usually without quotes, and strings to be
matched <code>'in single quotes'</code>.)

<P>
Some characters, like "<tt class="character">|</tt>" or "<tt class="character">(</tt>", are special.  Special
characters either stand for classes of ordinary characters, or affect
how the regular expressions around them are interpreted.

<P>
The special characters are:

<P>

<DL COMPACT>
<DT>"<tt class="character">.</tt>"
<DD>(Dot.)  In the default mode, this matches any
character except a newline.  If the <tt class="constant">DOTALL</tt> flag has been
specified, this matches any character including a newline.

<P>
<DT>"<tt class="character">^</tt>"
<DD>(Caret.)  Matches the start of the string, and in
<tt class="constant">MULTILINE</tt> mode also matches immediately after each newline.

<P>
<DT>"<tt class="character">$</tt>"
<DD>Matches the end of the string, and in
<tt class="constant">MULTILINE</tt> mode also matches before a newline.
<tt class="regexp">foo</tt> matches both 'foo' and 'foobar', while the regular
expression <tt class="regexp">foo$</tt> matches only 'foo'.

<P>
<DT>"<tt class="character">*</tt>"
<DD>Causes the resulting RE to
match 0 or more repetitions of the preceding RE, as many repetitions
as are possible.  <tt class="regexp">ab*</tt> will
match 'a', 'ab', or 'a' followed by any number of 'b's.

<P>
<DT>"<tt class="character">+</tt>"
<DD>Causes the
resulting RE to match 1 or more repetitions of the preceding RE.
<tt class="regexp">ab+</tt> will match 'a' followed by any non-zero number of 'b's; it
will not match just 'a'.

<P>
<DT>"<tt class="character">?</tt>"
<DD>Causes the resulting RE to
match 0 or 1 repetitions of the preceding RE.  <tt class="regexp">ab?</tt> will
match either 'a' or 'ab'.
<DT><code>*?</code>, <code>+?</code>, <code>??</code>
<DD>The "<tt class="character">*</tt>", "<tt class="character">+</tt>", and
"<tt class="character">?</tt>" qualifiers are all <i class="dfn">greedy</i>; they match as much text as
possible.  Sometimes this behaviour isn't desired; if the RE
<tt class="regexp">&lt;.*&gt;</tt> is matched against <code>'&lt;H1&gt;title&lt;/H1&gt;'</code>, it will match the
entire string, and not just <code>'&lt;H1&gt;'</code>.
Adding "<tt class="character">?</tt>" after the qualifier makes it perform the match in
<i class="dfn">non-greedy</i> or <i class="dfn">minimal</i> fashion; as <i>few</i> characters as
possible will be matched.  Using <tt class="regexp">.*?</tt> in the previous
expression will match only <code>'&lt;H1&gt;'</code>.

<P>
<DT><code>{<var>m</var>,<var>n</var>}</code>
<DD>Causes the resulting RE to match from
<var>m</var> to <var>n</var> repetitions of the preceding RE, attempting to
match as many repetitions as possible.  For example, <tt class="regexp">a{3,5}</tt>
will match from 3 to 5 "<tt class="character">a</tt>" characters.  Omitting <var>n</var>
specifies an infinite upper bound; you can't omit <var>m</var>.

<P>
<DT><code>{<var>m</var>,<var>n</var>}?</code>
<DD>Causes the resulting RE to
match from <var>m</var> to <var>n</var> repetitions of the preceding RE,
attempting to match as <i>few</i> repetitions as possible.  This is
the non-greedy version of the previous qualifier.  For example, on the
6-character string <code>'aaaaaa'</code>, <tt class="regexp">a{3,5}</tt> will match 5
"<tt class="character">a</tt>" characters, while <tt class="regexp">a{3,5}?</tt> will only match 3
characters.

<P>
<DT>"<tt class="character">&#92;</tt>"
<DD>Either escapes special characters (permitting
you to match characters like "<tt class="character">*</tt>", "<tt class="character">?</tt>", and so
forth), or signals a special sequence; special sequences are discussed
below.

<P>
If you're not using a raw string to
express the pattern, remember that Python also uses the
backslash as an escape sequence in string literals; if the escape
sequence isn't recognized by Python's parser, the backslash and
subsequent character are included in the resulting string.  However,
if Python would recognize the resulting sequence, the backslash should
be repeated twice.  This is complicated and hard to understand, so
it's highly recommended that you use raw strings for all but the
simplest expressions.

<P>
<DT><code>[]</code>
<DD>Used to indicate a set of characters.  Characters can
be listed individually, or a range of characters can be indicated by
giving two characters and separating them by a "<tt class="character">-</tt>".  Special
characters are not active inside sets.  For example, <tt class="regexp">[akm$]</tt>
will match any of the characters "<tt class="character">a</tt>", "<tt class="character">k</tt>",
"<tt class="character">m</tt>", or "<tt class="character">$</tt>"; <tt class="regexp">[a-z]</tt>
will match any lowercase letter, and <code>[a-zA-Z0-9]</code> matches any
letter or digit.  Character classes such as <code>&#92;w</code> or <code>&#92;S</code>
(defined below) are also acceptable inside a range.  If you want to
include a "<tt class="character">]</tt>" or a "<tt class="character">-</tt>" inside a set, precede it with a
backslash, or place it as the first character.  The 
pattern <tt class="regexp">[]]</tt> will match <code>']'</code>, for example.  

<P>
You can match the characters not within a range by <i class="dfn">complementing</i>
the set.  This is indicated by including a
"<tt class="character">^</tt>" as the first character of the set; "<tt class="character">^</tt>" elsewhere will
simply match the "<tt class="character">^</tt>" character.  For example, <tt class="regexp">[^5]</tt>
will match any character except "<tt class="character">5</tt>".

<P>
<DT>"<tt class="character">|</tt>"
<DD><code>A|B</code>, where A and B can be arbitrary REs,
creates a regular expression that will match either A or B.  An
arbitrary number of REs can be separated by the "<tt class="character">|</tt>" in this
way.  This can be used inside groups (see below) as well.  REs
separated by "<tt class="character">|</tt>" are tried from left to right, and the first
one that allows the complete pattern to match is considered the
accepted branch.  This means that if <code>A</code> matches, <code>B</code> will
never be tested, even if it would produce a longer overall match.  In
other words, the "<tt class="character">|</tt>" operator is never greedy.  To match a
literal "<tt class="character">|</tt>", use <tt class="regexp">&#92;|</tt>, or enclose it inside a
character class, as in <tt class="regexp">[|]</tt>.

<P>
<DT><code>(...)</code>
<DD>Matches whatever regular expression is inside the
parentheses, and indicates the start and end of a group; the contents
of a group can be retrieved after a match has been performed, and can
be matched later in the string with the <tt class="regexp">&#92;<var>number</var></tt> special
sequence, described below.  To match the literals "<tt class="character">(</tt>" or
"<tt class="character">)</tt>", use <tt class="regexp">&#92;(</tt> or <tt class="regexp">&#92;)</tt>, or enclose them
inside a character class: <tt class="regexp">[(] [)]</tt>.

<P>
<DT><code>(?...)</code>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -