title: java regular expression find a string add it to array and then replace original
body: <p>I have a string of text like this:</p>
  
  <pre><code>This is a[WAIT] test.
  </code></pre>
  
  <p>What I want to do is search the string for a substring that starts with [ and ends with ]
  Each one I find I want to add it to an ArrayList and replace substring in original string with a ^</p>
  
  <p>Here is my regex: </p>
  
  <pre><code>String regex_script = "/^\\[\\]$/"; //Match a string which starts with the character [ ending in the character ] 
  </code></pre>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>StringBuffer sb = new StringBuffer();
  
  Pattern p = Pattern.compile(regex_script); // Create a pattern to match
  Matcher m = p.matcher(line);  // Create a matcher with an input string
  boolean result = m.find();
          while(result) {
                  m.appendReplacement(sb, "^");
                  result = m.find();
          }
          m.appendTail(sb); // Add the last segment of input to the new String
  </code></pre>
  
  <p>how would I got about doing this? Thanks </p>