How to write Regular Expression for recursive matching?

Boris Borcic bborcic at gmail.com
Mon Nov 26 18:01:04 CET 2007


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?
> 
> Thanks,
> 

Do you need it to be a regular expression ?

 >>> def f(s) :
	ws = s.split('.')
	return map('.'.join,zip(ws,ws[1:]))

 >>> f('abc.defg.hij.klmnop')
['abc.defg', 'defg.hij', 'hij.klmnop']




More information about the Python-list mailing list