How to write Regular Expression for recursive matching?

lisong lisong.1979 at gmail.com
Mon Nov 26 13:30:30 EST 2007


On Nov 26, 12:34 pm, "J. Clifford Dyer" <j... at sdf.lonestar.org> wrote:
> On Mon, Nov 26, 2007 at 06:04:54PM +0100, Diez B. Roggisch wrote regarding Re: How to write Regular Expression for recursive matching?:
>
>
>
>
>
> > lisong wrote:
>
> > > Hi All,
>
> > > I have problem to split a string like this:
>
> > > 'abc.defg.hij.klmnop'
>
> > > and I want to get all substrings with only one '.' in mid. so the
> > > output I expect is :
>
> > > 'abc.defg', 'defg.hij', 'hij.klmnop'
>
> > > a simple regular expression '\w+.\w' will only return:
> > > 'abc.defg', 'hij.klmnop'
>
> > > is there a way to get 'defg.hij' using regular expression?
>
> > Nope. Regular expressions can't get back in their input-stream, at least not
> > for such stuff.
>
> > The problem at hand is easily solved using
>
> > s = 'abc.defg.hij.klmnop'
>
> > pairs = [".".join(v) for v in zip(s.split(".")[:-1], s.split(".")[1:])]
>
> which is veritably perlesque in its elegance and simplicity!
>
> A slightly more verbose version.
>
> l = s.split('.')
> pairs = []
> for x in xrange(len(l)-1):
>         pairs.append('.'.join(l[x:x+2]))
>
> Cheers,
> Cliff

Thank u all for your kindly reply, I agree, RE is not necessary here.

Song



More information about the Python-list mailing list