fork download
  1. import re
  2. sentence = ['if', 'it', 'will', 'rain', ',', 'if', 'will', 'stay', 'at', ',', 'home']
  3. s = ' '.join(sentence)
  4. p = re.compile("((\\bif\\b)[a-zA-z0-9\\'\\s]+)\\s*(,*)\\s*(then|,)")
  5. arr = re.findall(p, s)
  6. pos = 0
  7. for i, x in enumerate(arr):
  8. start = sentence.index(x[1], pos)
  9. end = sentence.index(x[3], pos)
  10. stri = ' '.join(sentence[start: end])
  11. print(stri)
  12. pos = sentence.index(x[3], pos) + 1
Success #stdin #stdout 0.02s 9984KB
stdin
Standard input is empty
stdout
if it will rain
if will stay at