position of matches

Peter Hansen peter at engcorp.com
Sat Jun 22 20:36:34 EDT 2002


Peter Hansen wrote:
> 
> les wrote:
> >
> > i am new to python and want to do the following:
> > given a string str='abcABCDefsgRSTVderDae'
> > i would like to find all the internal lowercase strings between 2 caps
> > i.e. pattern=re.compile(r'[A-Z]([a-z]+)[A-Z]')
> >
> > match_obj=pattern.search(str)
> > begin,end=match_obj.span()
> >
> > however i would like to get all the begining and end positions
> > of the pattern,
> > i.e.
> > efsg  begin=7 end=10
> > der   begin=15 end 17
> 
> Try this:
> >>> for x in m.finditer(s):
> ...   print '%s\tbegin=%s end=%s' % ((x.group(1),) + x.span(1))

Oops, if it wasn't obvious, "m" is "pattern" from above...
sorry.

-Peter



More information about the Python-list mailing list