1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.2.9: http://docutils.sourceforge.net/" />
<title>Module nre</title>
<link rel="stylesheet" href="default.css" type="text/css" />
</head>
<body>
<div class="document" id="module-nre">
<h1 class="title">Module <tt class="literal"><span class="pre">nre</span></tt></h1>
<p>nre - new style regular expressions</p>
<p>The <tt class="literal"><span class="pre">nre</span></tt> module adopt an object-oriented approach to regular expressions.
It provides only a class, <tt class="literal"><span class="pre">Regexp</span></tt>, which instances are regular expression
objects, or reobj for short. It can be imported with</p>
<blockquote>
<pre class="doctest-block">
>>> from nre import Regexp
</pre>
</blockquote>
<p>(notice that <tt class="literal"><span class="pre">from</span> <span class="pre">nre</span> <span class="pre">import</span> <span class="pre">*</span></tt> would have the same effect).</p>
<div class="section" id="classes">
<h1><a name="classes">Classes</a></h1>
<p><tt class="literal"><span class="pre">class</span> <span class="pre">Regexp(object):</span></tt></p>
<blockquote>
<p>The class of regular expression objects. Instantiated with the signature</p>
<p>reobj=Regexp(pattern,name=None,doc=None,isgroup=False)</p>
<p>pattern is a (raw) string which can be associated with a valid
regular expression; it the pattern contains an sequence ' #',
only the chars on the left of ' #' are retained, whereas all
the chars on the right of ' #' are assumed to be a comment.
If you want to avoid this interpretation, you must escape #.
Then the comment is stripped with the pattern and assigned with
the docstring, unless an explicit docstring was already provided.
if name is not None, associates a name to the reobj;
if doc is not None, associates a docstring to the reobj;
if isgroup is True, converts the regexp in a named group
<tt class="literal"><span class="pre">(?P<name>regexp)</span></tt> or an numbered group <tt class="literal"><span class="pre">(regexp)</span></tt> if
name is None.</p>
<p>re-objects are callable. For instance</p>
<pre class="doctest-block">
>>> a=Regexp('x # named group')('a')
</pre>
<p>returns a named group regular expression, whereas</p>
<pre class="doctest-block">
>>> b=Regexp('x # numbered group')
</pre>
<p>returns a numbered group regexp.</p>
<pre class="doctest-block">
>>> print a
>>> print b
</pre>
<p><tt class="literal"><span class="pre">compose(self,other,oper):</span></tt></p>
<blockquote>
Compose regular expression objects with strings or with themselves.
<tt class="literal"><span class="pre">oper</span></tt> is one of the strings "__add__", "__radd__", "__or__",
"__ror__".</blockquote>
<p><tt class="literal"><span class="pre">__ror__(self,other):</span></tt></p>
<blockquote>
<p>Combines a regular expression object to a string
or another reobj using <tt class="literal"><span class="pre">|</span></tt> . Works from the right. Example:</p>
<pre class="doctest-block">
>>> print 'x' | Regexp('y')
<reobj '<noname>':x|y>
</pre>
</blockquote>
<p><tt class="literal"><span class="pre">__radd__(self,other):</span></tt></p>
<blockquote>
<p>Adds a regular expression object to a string or another
reobj. Works from the right. Example:</p>
<pre class="doctest-block">
>>> print 'x' + Regexp('y')
<reobj '<noname>':xy>
</pre>
</blockquote>
<p><tt class="literal"><span class="pre">__or__(self,other):</span></tt></p>
<blockquote>
<p>Combines a regular expression object to a string
or to another reobj using <tt class="literal"><span class="pre">|</span></tt> . Works from the left. Example:</p>
<pre class="doctest-block">
>>> print Regexp('x') | 'y'
<reobj '<noname>':x|y>
</pre>
<p>Works well with named reobj too:</p>
<pre class="doctest-block">
>>> a=Regexp('x',name='a')
>>> b=Regexp('y',name='b')
>>> print a|b
<reobj 'a__b':x|y>
</pre>
</blockquote>
<p><tt class="literal"><span class="pre">__add__(self,other):</span></tt></p>
<blockquote>
<p>Adds a regular expression object to a string or
to another reobj. Works from the left. Example:</p>
<pre class="doctest-block">
>>> print Regexp('x') + 'y'
<reobj '<noname>':xy>
</pre>
<p>Works well with named reobj too:</p>
<pre class="doctest-block">
>>> a=Regexp('x',name='a')
>>> b=Regexp('y',name='b')
>>> print a+b
<reobj 'a_b':xy>
</pre>
</blockquote>
<p><tt class="literal"><span class="pre">__call__(self,name=None):</span></tt></p>
<blockquote>
Returns a grouped regular expression object. The group is named
if the regexp was already named or if an explicit name is passed.</blockquote>
</blockquote>
</div>
</div>
<hr class="footer"/>
<div class="footer">
<a class="reference" href="nre.rst">View document source</a>.
Generated on: 2003-09-23 10:57 UTC.
Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
</div>
</body>
</html>
|