title: How to match alternatives with python regex
body: <p>given string 1:</p>
  
  <p>'''TOM
  likes to go swimming
  MARY 
  loves to go to the playground
  JANE
  likes going shopping'''</p>
  
  <p>I want to capture the text between only 2 names. Either Tom and Mary or Tom and Jane. If Mary appears before Jane, I would like to capture the text between Tom and Mary. However, if Jane appears first, I would like to capture the text between Tom and Jane. </p>
  
  <p>I have written the following code:</p>
  
  <pre><code>text = re.compile(r'''(
              TOM\s*
              ([\w\W]+)\s*
              JANE|MARY
              )''', re.VERBOSE)
  
  text_out = text.search(string).group(1)
  </code></pre>
  
  <p>However, this code would give me the text between Tom and Jane, even though Mary appears first. I understand that this is because the pipe function reads from left to right and therefore will match Jane first. Is there a way to code this such that it depends on who appears first in the text? </p>
  
  <p>for example, in string2:
  "'''TOM
  likes to go swimming
  JANE
  likes going shopping
  MARY 
  loves to go to the playground
  '''</p>
  
  <p>I would like to capture the text between Tom and Jane for string2. </p>