[Tutor] re syntax

Tiago Saboga tiagosaboga at terra.com.br
Mon Aug 28 15:26:00 CEST 2006


A couple of weeks ago I asked about a way to parse man pages, and Danny and 
Alan answered me with some tips (thanks). I've tryed doclifter, which I 
already knew, but could not yet master it; in fact, it just doesn't work with 
many of the man-pages I tried in my debian system. Anyway, I'm refreshing my 
mind about regular expressions in python, and while trying to document my 
steps, I found a weird syntax problem. The problem is: why the hell do I need 
to escape special caracters when in verbose mode? And why isn't it made clear 
on the re module doc page ( http://docs.python.org/lib/module-re.html ).

I'll just paste below (or above? I never know how to say this in english) my 
ipython's session, you'll see what I mean.

But before that, an extra question to tutors: do you have some advice for 
people like me who like to program but can't spend enough time doing it?

...it's a joke...

In [18]: re.compile("ab").match("abc").group()
Out[18]: 'ab'

In [19]: re.compile("ab", re.X).match("abc").group()
Out[19]: 'ab'

In [20]: re.compile("a\tb").match("a\tbc").group()
Out[20]: 'a\tb'

In [21]: re.compile("a\tb", re.X).match("a\tbc").group()
---------------------------------------------------------------------------
exceptions.AttributeError                            Traceback (most recent 
call last)

/home/tiago/<ipython console>

AttributeError: 'NoneType' object has no attribute 'group'

In [22]: re.compile("a\tb", re.X).match(r"a\tbc").group()
---------------------------------------------------------------------------
exceptions.AttributeError                            Traceback (most recent 
call last)

/home/tiago/<ipython console>

AttributeError: 'NoneType' object has no attribute 'group'

In [23]: re.compile(r"a\tb", re.X).match("a\tbc").group()
Out[23]: 'a\tb'

In [24]: re.compile("a\\tb", re.X).match("a\tbc").group()
Out[24]: 'a\tb'


More information about the Tutor mailing list