#! /usr/bin/env python import re from collections import deque pattern = r'([AZ]{2,}(?=[AZ]|$)|[AZ](?=[az]|$))' chunks = deque(re.split(pattern, 'HDMWhoSomeMONKEYThingXYZ')) result = [] while len(chunks): buf = chunks.popleft() if len(buf) == 0: continue if re.match(r'^[AZ]$', buf) and len(chunks): buf += chunks.popleft() result.append(buf) print ' '.join(result)
Conclusion:
HDM Who Some MONKEY Thing XYZ
Judging by the lines of code, this task is much more natural for re.findall :
pattern = r'([AZ]{2,}(?=[AZ]|$)|[AZ][az]*)' print ' '.join(re.findall(pattern, 'HDMWhoSomeMONKEYThingX'))
Conclusion:
HDM Who Some MONKEY Thing X
source share