Re: Self referential regular expressions with burried bindings.

Tim Peters (tim@ksr.com)
Wed, 09 Mar 94 21:41:23 EST

> import string
> two_char = []
> for item in string.splitfields(a, '%')[1:]:
> two_char.append(item[:2])

Or two_char = map(lambda s:s[:2], string.splitfields(str, '%')[1:])
does the same thing without the loop.

David,

1) Regular expressions aren't powerful enough to solve your problem
without a loop, and if you have to use a loop anyway the 'string'
module operations are probably faster.

2) The problem statement may or may not have assumed that a '%' is never
one of the two characters following a '%'. If a '%' _can_ be one of
the following two characters, the solution above will surprise you.
In that case, this may not (or may ...):

from string import find
def p2(str):
answer = []
while str:
i = find(str,'%')
if i < 0: break
answer.append( str[i+1:i+3] )
str = str[i+3:]
return answer

E.g., given '%%%ab', this will return ['%%']. The solution
above will return ['', '', 'ab']. Another possibility is that you
want ['%%', '%a', 'ab']. Another is that you don't care ...

regexps-are-death-on-an-ill-defined-problem-ly y'rs - tim

Tim Peters tim@ksr.com
not speaking for Kendall Square Research Corp