title: Algorithm to insert string based on multiple regex rules
body: <p>I am creating a Markdown-esque markup interface.
  For example, when the user types in <code>**example string**</code>, regex is used to find the two occurrences of <code>**</code> (defining bold text), and the actual plaintext will be changed to <code>&lt;b&gt;**example string**&lt;/b&gt;</code> and rendered as HTML. </p>
  
  <p>This is my thinking for parsing the user's input into HTML:</p>
  
  <ol>
  <li>For each rule in regex rules</li>
  <li>For each occurrence of <code>start pattern</code> (of current regex rule)</li>
  <li>Take all text <em>after</em> the end of <code>start pattern</code> (call this <code>start substring</code>)</li>
  <li>For first instance of <code>end pattern</code> in <code>start substring</code></li>
  <li>Take <code>substring(start_match.start() + end_match.end())</code> from text</li>
  <li>Append it to an initially blank <code>final text</code> string</li>
  <li>Cull remaining text via <code>substring(start_match.start() + end_match.end())</code>, feed this back into the text read at <strong>2.</strong></li>
  </ol>
  
  <p>My code:</p>
  
  <pre><code>public static String process(String input_text) {
      String final_text = "";
      String current_text = input_text;
  
      for (MarkdownRule rule : _rules) {
          Pattern s_ptrn = rule.getStartPattern();    // Start pattern
          Pattern e_ptrn = rule.getEndPattern();      // End pattern
  
          /* For each occurrence of the start pattern */
          Matcher s_matcher = s_ptrn.matcher(current_text);
          while (s_matcher.find()) {
              int s_end = s_matcher.end();
              int s_start = s_matcher.start();
  
              /* Take all text after the end of start match */
              String working_text = current_text.substring(s_end); // ERROR HERE
  
              /* For first instance of end pattern in remaining text */
              Matcher e_matcher = e_ptrn.matcher(working_text);
              if (e_matcher.find()) {
  
                  /* Take full substring from current text */
                  int e_end = e_matcher.end();
                  working_text = current_text.substring(s_start, s_end + e_end);
  
                  /* Append to final text */
                  working_text = new StringBuilder(working_text).insert(0, "&lt;b&gt;").append("&lt;/b&gt;").toString();
                  final_text = new StringBuilder(final_text).append(working_text).toString();
  
                  /* Remove working text from current text */
                  current_text = new StringBuilder(current_text).substring(s_start + e_end);
              }
          }
      }
  
      return final_text;
  }
  </code></pre>
  
  <p>Although theoretically this should work fine, I'm getting a <code>StringIndexOutOfBoundsException</code> on this line:</p>
  
  <pre><code>/* Take all text after the end of start match */
  String working_text = current_text.substring(s_end);
  </code></pre>
  
  <p>When I'm using the input text <code>**example**</code>. I believe it works fine for the first occurrence of <code>start pattern</code> (at indices 0 &amp; 1), but then the string doesn't get culled properly and the loop is then called on the plaintext <code>**</code>, this giving the out of range error. (I can't guarantee this, though -- its just what I believe from my own testing)</p>
  
  <p>Unfortunately, my troubleshooting could not remediate the error. Thanks in advance for any help.</p>