[Tutor] module re
dyoo at hkn.eecs.berkeley.edu
Sat May 1 20:16:56 EDT 2004
On Sat, 1 May 2004, Magnus [iso-8859-1] Lyck=E5 wrote:
> I'm not sure what you did, because re substitute functions return
> strings. Deletion is the same as replacing with "", right?
Marina used re.compile(), which returns a 'regex' object. I think there's
some confusion in the usage:
> > I used module re.compile(text, re.X).
What Marina probably wants to do is first compile a pattern, and then
apply that pattern to text. For example, if we have something like this:
>>> space_pattern =3D re.compile(' ')
then what we are holding now is a regular expression "pattern" object that
knows how to look at spaces:
<_sre.SRE_Pattern object at 0x60a20>
This 'space' pattern object can be applied to things like string
>>> space_pattern.sub('*', 'hello world this is a test')
But note that trying to re.compile() the text we're trying to manipulate
isn't too effective:
>>> pattern =3D re.compile('hello world this is a test') ## ??
because we're inadvertantly reversing the roles of the text and the
pattern. We need to make a distinction between the "pattern" we're trying
to recognize, and the "text" we want to apply the pattern on. Maybe this
is the point of confusion; I'm not sure about this, though, since I
haven't seen more of Marina's program.
Marina, have you seen the Regex HOWTO tutorial yet? It has quite a few
examples of doing regular expressions in Python, written by A.M. Kuchling.
It's quite nice, and may help clear things up.
Here's a link to the tutorial:
More information about the Tutor