[Tutor] Regular Expression Misunderstanding

Steve Nelson sanelson at gmail.com
Fri Jul 14 11:02:38 CEST 2006


Hello,

I am reading the "Regular Expression HOWTO" at
http://www.amk.ca/python/howto/regex/

I am at the bit where "greediness" is discussed with respect to
metacharacters enabling repetition of sections of a RE.  I understand
the concept.

The author gives a step by step example of how the matching engine
goes through the RE step by step, and when the repitition
metacharacter appears it tries the maximum first, and then effectively
reels back until the last step of the RE will pass.

This made sense after a bit of time with pen and paper.

What I don't understand is how in the end the RE *does* actually match
- which may indicate a serious misunderstanding on my part.

>>> re.match("a[bcd]*b", "abcbd")
<_sre.SRE_Match object at 0x186b7b10>

I don't see how abcbd matches! It ends with a d and the RE states it
should end with a b!

What am I missing?

S.


More information about the Tutor mailing list