Negation in regular expressions
Ant
antroy at gmail.com
Fri Sep 8 10:44:12 EDT 2006
> >>> re.split(r'@#', s)
> ['', ' This ', ' is a ', '', ' test ']
> >>> [g.group(1) for g in re.finditer(r'(.*?)(?:@#|$)', s)]
> ['', ' This ', ' is a ', '', ' test ', '']
If it's duplicating the behaviour of split, but returning an iterator
instead, how about avoiding hacking around with messy regexes and use
something like the following generator:
def splititer(pattern, string):
posn = 0
while True:
m = pattern.search(string, posn)
if not m:
break
yield string[posn:m.start()]
posn = m.end()
More information about the Python-list
mailing list