fork download
  1. import re
  2.  
  3. dict = {}
  4.  
  5. regex = r"\bQuestion\s+\d+\s+(\S.*?)\s+Answer\s+\d+\s+(\S.*?)\s*(?=Question|$)"
  6. s = "Question 1 What is the weather today? Answer 1 It is hot and sunny. Question 2 What day is it today? Answer 2 Thursday Question 3 How many legs does a dog have? Answer 3 Four legs"
  7.  
  8. for m in re.findall(regex, s):
  9. dict[m[0]] = m[1]
  10. print(dict)
Success #stdin #stdout 0.02s 9584KB
stdin
Standard input is empty
stdout
{'What is the weather today?': 'It is hot and sunny.', 'What day is it today?': 'Thursday', 'How many legs does a dog have?': 'Four legs'}