[Tutor] Regular Expression Misunderstanding

John Fouhy john at fouhy.net
Fri Jul 14 11:15:44 CEST 2006


On 14/07/06, Steve Nelson <sanelson at gmail.com> wrote:
> 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?

It doesn't have to match the _whole_ string.

[bcd]* will match, amongst other things, the empty string (ie: 0
repetitions of either a b, a c, or a d).  So "a[bcd]*b" will match
"ab", which is in the string abcbd.

It will also match "abcb", which is the longest match, and thus
probably the one it found.

If you look at the match object returned, you should se that the match
starts at position 0 and is four characters long.

Now, if you asked for "a[bcd]*b$", that would be a different matter!

HTH :-)

-- 
John.


More information about the Tutor mailing list