title: Java Regex: Match text between two strings with boundary conditions
body: <p>I want to match text between two Strings, but the starting String has strict boundary conditions.</p>
  
  <p>Sample input:</p>
  
  <pre><code>start
  From: h
  From:b
   xyz
  Subject: 
  end
  </code></pre>
  
  <p>I need to match between <code>From:</code> and <code>Subject:</code>.</p>
  
  <p>If I use <code>(From:.*).*(Subject:)</code> with dotall, it produces</p>
  
  <pre><code>From: h
  From:b
   xyz
  Subject:
  </code></pre>
  
  <p>but I need only </p>
  
  <pre><code>From:b
   xyz
  Subject:
  </code></pre>
  
  <p>because the starting string has strict boundary conditions. This is necessary because the starting String could be anywhere in the document, and then the above regex will match a big text rather than just few lines.</p>
  
  <p>%%%%%%%%%%%% Problem redefined %%%%%%%%%%%%%%
  I have text in which I need to match:</p>
  
  <pre><code>From:&lt;any text&gt;
  To:&lt;any text&gt;
  Subject:&lt;any text&gt;
  </code></pre>
  
  <p>The catch is that: All the three components can be in one line, could be separated by one newline, or could be separated by 2 newlines... There are text before and after the desired match which could contain <code>From:&lt;any text&gt;</code>, that's why I need strict boundaries.</p>