Paddy wrote:
> George Sakkis wrote:
> > It's always striked me as odd that you can express negation of a single
> > character in regexps, but not any more complex expression. Is there a
> > general way around this shortcoming ? Here's an example to illustrate a
> > use case:
> >
> > >>> import re
> > # split with '@' as delimiter
> > >>> [g.group() for g in re.finditer('[EMAIL PROTECTED]', 'This @ is a @ 
> > >>> test ')]
> > ['This ', ' is a ', ' test ']
> >
> > Is it possible to use finditer to split the string if the delimiter was
> > more than one char long (say 'XYZ') ? [yes, I'm aware of re.split, but
> > that's not the point; this is just an example. Besides re.split returns
> > a list, not an iterator]
> >
> > George
>
> If your wiling to use groups then the following will split
>
> >>> [g.group(1) for g in re.finditer(r'(.+?)(?:@#|$)', 'This @# is a @# test 
> >>> ')]
> ['This ', ' is a ', ' test ']
>
> - Paddy.

Here is another wrapping of the same finditer call that just allows you
to call .group() on the result

>>> class G(object):
...     def __init__(self, x):
...             def grp(x=x):
...                     return x
...             self.group = grp
...
>>> [g.group() for g in (G(g.group(1)) for g in re.finditer(r'(.+?)(?:@#|$)', 
>>> 'This @# is a @# test '))]
['This ', ' is a ', ' test ']
>>> 

- Paddy.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to