title: How can I get a string between 2 asterisks?
body: <p>So I was looking at reddit and was wondering how they managed to do the formatting thing with the bold and the italic via asterisks encapsulating text, so I tried replicating that effect in java. I tried a lot of things and the following code is a scramble/mix/mashup of everything I tried</p>
  
  <pre><code>  public static ArrayList&lt;String&gt; redditReformat(String message) {
      ArrayList&lt;String&gt; single = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; doubl = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; fin = new ArrayList&lt;&gt;();
  
      if (message.contains("*") &amp;&amp; !message.contains("**")) {
          ArrayList&lt;Integer&gt; asterisks = new ArrayList&lt;&gt;();
  
          for (int index = message.indexOf("*"); index &gt;= 0; index = message.indexOf("*", index + 1)) {
              asterisks.add(index);
          }
  
          HashMap&lt;Integer, Integer&gt; patterns = new HashMap&lt;&gt;();
          for (int start : asterisks) {
              for (int end : asterisks) {
                  if (start &lt; end) {
                      if ((start + 1) != end) {
                          if (!patterns.containsKey(start) &amp;&amp; !patterns.containsValue(end)) {
                              single.add(message.substring(start, end));
                              patterns.put(start, end);
                          }
                      } else {
                          if (message.charAt(start + 1) == message.charAt(start)) {
                              if (message.charAt(end + 1) == message.charAt(end)) {
  
                                  ArrayList&lt;Integer&gt; doubleasterisks = new ArrayList&lt;&gt;();
  
                                  if (!patterns.containsKey(start) &amp;&amp; !patterns.containsValue(end)) {
                                      doubl.add(message.substring(start + 2, end - 1));
                                      if (doubleasterisks.size() &lt; 2) {
                                          doubleasterisks.add(start + end);
                                      } else {
                                          patterns.put(doubleasterisks.get(1), doubleasterisks.get(2));
                                          doubleasterisks.clear();
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  
      for (String s : single) {
          fin.add(s);
      }
      for (String d : doubl) {
          fin.add(d);
      }
      return fin;
  }
  </code></pre>
  
  <p>Yes it's messy and probably horrible, I've tried simple and complex things but this isn't going well. The method is supposed to return the asterisks between the a message as well, so like "haha *lol* potato" (stackoverflow formats the asterisks)</p>
  
  <p>It currently returns "*lol" instead of "*lol*" and it just doesn't work when done with **</p>
  
  <p>It also fails when there's more than one encapsulation.</p>