title: How to use a regex to search backwards effectively?
body: <p>I'm searching forward in an array of strings with a regex, like this:</p>
  
  <pre><code>for (int j = line; j &lt; lines.length; j++) {  
      if (lines[j] == null || lines[j].isEmpty()) {
          continue;
      }
      matcher = pattern.matcher(lines[j]);
      if (matcher.find(offset)) {
          offset = matcher.end();
          line = j;
          System.out.println("found \""+matcher.group()+"\" at line "+line+" ["+matcher.start()+","+offset+"]");
          return true;
      }
      offset = 0;
  }
  return false;
  </code></pre>
  
  <p>Note that in my implementation above I save the <code>line</code> and <code>offset</code> for continuous searches.</p>
  
  <p>Anyway, now I want to <strong>search backwards</strong> from that [line,offset].</p>
  
  <h3>My question: is there a way to search backwards with a regex efficiently? if not, what could be an alternative?</h3>
  
  <p><strong>Clarification:</strong> By <em>backwards</em> I mean finding the previous match.<br>
  For example, say that I'm searching for "dana" in </p>
  
  <pre><code>"dana nama? dana kama! lama dana kama?" 
  </code></pre>
  
  <p>and got to the 2nd match. If I do <code>matcher.find()</code> again, I'll search <em>forward</em> and get the 3rd match. But I want to search <em>backwards</em> and get to the 1st match.<br>
  the code above should then output something like:</p>
  
  <pre><code>found "dana" at line 0 [0,3] // fwd
  found "dana" at line 0 [11,14] // fwd
  found "dana" at line 0 [0,3] // bwd
  </code></pre>