How to write this repeat matching?

rxjwg98 at rxjwg98 at
Sun Jul 6 20:57:50 CEST 2014

On Python website, it says that the following match can reach 'abcb' in 6 steps:

A step-by-step example will make this more obvious. Let's consider the expression
a[bcd]*b. This matches the letter 'a', zero or more letters from the class [bcd], 
and finally ends with a 'b'. Now imagine matching this RE against the string 

The end of the RE has now been reached, and it has matched abcb.  This 
demonstrates how the matching engine goes as far as it can at first, and if no
match is found it will then progressively back up and retry the rest of the RE
again and again. It will back up until it has tried zero matches for [bcd]*, and
if that subsequently fails, the engine will conclude that the string doesn't
match the RE at all.

I write the following code:

import re

line = "abcdb"

matchObj = re.match( 'a[bcd]*b', line) 

if matchObj:
   print " : ",
   print " : ",
   print " : ",
   print " : ",
   print "No match!!"

In which I have used its match pattern, but the result is not 'abcb'

Only abcdb 

displays. All other group(s) have no content.

How to write this greedy search?


More information about the Python-list mailing list