How to write Regular Expression for recursive matching?

Paul McGuire ptmcg at austin.rr.com
Mon Nov 26 12:19:52 EST 2007


On Nov 26, 10:51 am, Paul McGuire <pt... at austin.rr.com> wrote:
> On Nov 26, 10:40 am, lisong <lisong.1... at gmail.com> 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?
>
> > Thanks,
>
> Why are you using regular expressions?  Use the split method defined
> for strings:
>
> >>> 'abc.defg.hij.klmnop'.split('.')
>
> ['abc', 'defg', 'hij', 'klmnop']
>
> -- Paul- Hide quoted text -
>
> - Show quoted text -

Sorry, misread your post - Diez Roggisch has the right answer.

-- Paul



More information about the Python-list mailing list