title: Ruby: determine whether a line is among the results of a regex match
body: <p>I have a fairly complex regular expression that matches parts of a document that lie between ASCII separators (e.g. ==================). I need to tell whether a given line from the document is one of the lines matched by this regex. So far my approach was to store the MatchData returned by matching my document against the regex, converting it to an array and iterating over it in order to find matches with the given line. </p>
  
  <pre><code>    between_separators = lambda { |ln, context| 
          body = context[:body]
          rx = /^(([=\*_]){23,}\2{3}(?:\2|[\r\n])+)([\s\S]+?)\1/
          matchdata = body.match(rx)
          matched_lines = matchdata.to_a.map { |m| m.split("\n") }.flatten
          matched_lines.each { |ml| return 1 if ml.match(ln) }
          return 0
      }
  </code></pre>
  
  <p>While it is not common, there are cases where I end up with false-positives, because the line I'm checking is the same as some other line that actually was among the results from the first match.</p>
  
  <p>Is there a smarter way of solving this problem?</p>
  
  <p>EDIT:</p>
  
  <p>Let me provide some more context.</p>
  
  <p>I am given a plaintext document that contains blocks of text enclosed by 'separator' lines. I want to check whether a line taken from the document is between such separators.</p>
  
  <p>Here's an example of what I'm dealing with:</p>
  
  <pre><code>    This is some text that should not be matched. As you can see, it is not enclosed
  by separator lines.
  
  ===========================================================
  This part should be matched as it is between two separator lines. Note that the
  opening and closing separators are composed of the exact same number of the same
  character.
  ===========================================================
  This block should not be matched as it is not enclosed by its own separators,
  but rather the closing separator of the previous block and the opening 
  separator of the next block.
  ===========================================================
  It is tricky to distinguish between an enclosed and non-enclosed blocks, because
  sometimes a matching pair of separators appears to be legal, while it is really
  the closing separator of the previous block and the opening separator of the
  next one (e.g. the block obove this one).
  ===========================================================
  ==================================
  =====
  This block is enclosed by multiline separators.
  ==================================
  =====
  Some more text that should not be matched by the regex.
  ***************************************
  
  
  
  A separator can use one of the following characters: '=' or '*' or '_'.
  
  
  ***************************************
  ***************************************
  *******************
  Another example of a multiline separated block.
  ***************************************
  *******************
  
  &gt;Even more text not to be matchedby the regex. This time, preceeded by a
  &gt;variable number of '&gt;'.
  &gt;&gt;__________________________________________
  &gt;&gt;And another type of separator. The block is now also a part of a reply section
  &gt;&gt;of the email.
  &gt;&gt;__________________________________________
  </code></pre>
  
  <p>My aim is to be able to call <code>between_separators["This block is enclosed by multiline separators.", context]</code> and get <code>1</code> as result. While the method I provided will succeed in most cases, it is unreliable and I want to improve it not to give false-positive results.</p>