Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-struct.html" /> | |
13 | <link rel="prev" href="module-string.html" /> | |
14 | <link rel="parent" href="strings.html" /> | |
15 | <link rel="next" href="re-syntax.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>4.2 re -- Regular expression operations</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="4.1.4 Deprecated string functions" | |
25 | href="node111.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="4. String Services" | |
28 | href="strings.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="4.2.1 Regular Expression Syntax" | |
31 | href="re-syntax.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="node111.html">4.1.4 Deprecated string functions</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="strings.html">4. String Services</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="re-syntax.html">4.2.1 Regular Expression Syntax</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION006200000000000000000"> | |
56 | 4.2 <tt class="module">re</tt> -- | |
57 | Regular expression operations</A> | |
58 | </H1> | |
59 | <A NAME="module-re"></A> | |
60 | ||
61 | <P> | |
62 | ||
63 | <P> | |
64 | This module provides regular expression matching operations similar to | |
65 | those found in Perl. Regular expression pattern strings may not | |
66 | contain null bytes, but can specify the null byte using the | |
67 | <code>\<var>number</var></code> notation. Both patterns and strings to be | |
68 | searched can be Unicode strings as well as 8-bit strings. The | |
69 | <tt class="module">re</tt> module is always available. | |
70 | ||
71 | <P> | |
72 | Regular expressions use the backslash character ("<tt class="character">\</tt>") to | |
73 | indicate special forms or to allow special characters to be used | |
74 | without invoking their special meaning. This collides with Python's | |
75 | usage of the same character for the same purpose in string literals; | |
76 | for example, to match a literal backslash, one might have to write | |
77 | <code>'\\\\'</code> as the pattern string, because the regular expression | |
78 | must be "<tt class="samp">\\</tt>", and each backslash must be expressed as | |
79 | "<tt class="samp">\\</tt>" inside a regular Python string literal. | |
80 | ||
81 | <P> | |
82 | The solution is to use Python's raw string notation for regular | |
83 | expression patterns; backslashes are not handled in any special way in | |
84 | a string literal prefixed with "<tt class="character">r</tt>". So <code>r"\n"</code> is a | |
85 | two-character string containing "<tt class="character">\</tt>" and "<tt class="character">n</tt>", | |
86 | while <code>"\n"</code> is a one-character string containing a newline. | |
87 | Usually patterns will be expressed in Python code using this raw | |
88 | string notation. | |
89 | ||
90 | <P> | |
91 | <div class="seealso"> | |
92 | <p class="heading">See Also:</p> | |
93 | ||
94 | <dl compact="compact" class="seetitle"> | |
95 | <dt><em class="citetitle" | |
96 | >Mastering Regular Expressions</em></dt> | |
97 | <dd>Book on regular expressions | |
98 | by Jeffrey Friedl, published by O'Reilly. The second | |
99 | edition of the book no longer covers Python at all, | |
100 | but the first edition covered writing good regular expression | |
101 | patterns in great detail.</dd> | |
102 | </dl> | |
103 | </div> | |
104 | ||
105 | <P> | |
106 | ||
107 | <p><br /></p><hr class='online-navigation' /> | |
108 | <div class='online-navigation'> | |
109 | <!--Table of Child-Links--> | |
110 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
111 | ||
112 | <UL CLASS="ChildLinks"> | |
113 | <LI><A href="re-syntax.html">4.2.1 Regular Expression Syntax</a> | |
114 | <LI><A href="matching-searching.html">4.2.2 Matching vs Searching</a> | |
115 | <LI><A href="node115.html">4.2.3 Module Contents</a> | |
116 | <LI><A href="re-objects.html">4.2.4 Regular Expression Objects</a> | |
117 | <LI><A href="match-objects.html">4.2.5 Match Objects</a> | |
118 | <LI><A href="node118.html">4.2.6 Examples</a> | |
119 | </ul> | |
120 | <!--End of Table of Child-Links--> | |
121 | </div> | |
122 | ||
123 | <DIV CLASS="navigation"> | |
124 | <div class='online-navigation'> | |
125 | <p></p><hr /> | |
126 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
127 | <tr> | |
128 | <td class='online-navigation'><a rel="prev" title="4.1.4 Deprecated string functions" | |
129 | href="node111.html"><img src='../icons/previous.png' | |
130 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
131 | <td class='online-navigation'><a rel="parent" title="4. String Services" | |
132 | href="strings.html"><img src='../icons/up.png' | |
133 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
134 | <td class='online-navigation'><a rel="next" title="4.2.1 Regular Expression Syntax" | |
135 | href="re-syntax.html"><img src='../icons/next.png' | |
136 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
137 | <td align="center" width="100%">Python Library Reference</td> | |
138 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
139 | href="contents.html"><img src='../icons/contents.png' | |
140 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
141 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
142 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
143 | <td class='online-navigation'><a rel="index" title="Index" | |
144 | href="genindex.html"><img src='../icons/index.png' | |
145 | border='0' height='32' alt='Index' width='32' /></A></td> | |
146 | </tr></table> | |
147 | <div class='online-navigation'> | |
148 | <b class="navlabel">Previous:</b> | |
149 | <a class="sectref" rel="prev" href="node111.html">4.1.4 Deprecated string functions</A> | |
150 | <b class="navlabel">Up:</b> | |
151 | <a class="sectref" rel="parent" href="strings.html">4. String Services</A> | |
152 | <b class="navlabel">Next:</b> | |
153 | <a class="sectref" rel="next" href="re-syntax.html">4.2.1 Regular Expression Syntax</A> | |
154 | </div> | |
155 | </div> | |
156 | <hr /> | |
157 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
158 | </DIV> | |
159 | <!--End of Navigation Panel--> | |
160 | <ADDRESS> | |
161 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
162 | </ADDRESS> | |
163 | </BODY> | |
164 | </HTML> |