Using regex , str.translate and str.split :
>>> import re >>> from string import whitespace >>> strs = re.search(r'List:(.*)(\s\S*\w+):', ph, re.DOTALL).group(1) >>> strs.translate(None, ':'+whitespace).split(',') ['username1', 'username2', 'username3', 'username4', 'username5']
Here you can also create a dict that allows you to access any attribute:
def func(lis): return ''.join(lis).translate(None, ':'+whitespace) lis = [x.split() for x in re.split(r'(?<=\w):',ph.strip(), re.DOTALL)] dic = {} for x, y in zip(lis[:-1], lis[1:-1]): dic[x[-1]] = func(y[:-1]).split(',') dic[lis[-2][-1]] = func(lis[-1]).split(',') print dic['List'] print dic['Members'] print dic['alias']
Output:
['username1', 'username2', 'username3', 'username4', 'username5'] ['User1', 'User2', 'User3', 'User4', 'User5'] ['tech.sw.host']
source share