fork download
  1. import re
  2. dummyText1 = "bill: hello \nted: hello \nto you \nbill: goodbye \nted: goodbye \nto you"
  3. #speakers = ['bill', 'ted']
  4. #p = fr'^({"|".join(speakers)}):(.*(?:\n(?!(?:{"|".join(speakers)})).*)*)'
  5. p = r'^((?:[^\W\d_]|[^\S\r\n])+):(.*(?:\n(?!(?:[^\W\d_]|[^\S\r\n])+:).*)*)'
  6. print( [[x.strip(),y.replace('\n','').strip()] for x,y in re.findall(p, dummyText1, re.M)])
Success #stdin #stdout 0.02s 9488KB
stdin
Standard input is empty
stdout
[['bill', 'hello'], ['ted', 'hello to you'], ['bill', 'goodbye'], ['ted', 'goodbye to you']]