title: Regex matching every character until a certain character ONLY if that certain character exists
body: <p>Basically, I'm reading information from the Wikipedia API, which returns JSON code containing the source code for a page in their markdown. I used a JSON API to filter what I want from the page, and now I want to format the text in a way that removes all the links and such.</p>
  
  <p>The markdown displays links like this: <code>[[wiki page|display text]]</code><br>
  But it can also display like this: <code>[[wiki page]]</code></p>
  
  <p>So, what I'm trying to do is extract the <code>display text</code> if the pipe character exists, but if not I just want the <code>wiki page</code> text.</p>
  
  <p>This is my code for that right now, which <em>should</em> detect if there's a pipe character and handle those strings properly but doesn't:</p>
  
  <pre><code>private static String format(String s) {
      return s.replaceAll("\\[\\[.+?(\\]\\]|\\|)", "").replace("[[", "").replace("]]", "").trim();
  }
  </code></pre>
  
  <p>When running this it will sometimes take out any text that displays as simply <code>[[wiki page]]</code>, but it works if the pipe character is there. How do I manage to get this working correctly?</p>