title: Java regex match markdown syntax for headings
body: <p>I have a string with markdown syntax in it, and I want to be able to find markdown syntax for headings, i.e h1 = #, h2 = ## etc etc.</p>
  
  <p>I know that whenever I find a heading, it is at the start of the line. I also know there can only be one heading per line. So for example, "###This is a heading" would match true for my h3 pattern, but not for my h2 or h1 patterns. This is my code so far:</p>
  
  <pre><code>h1 = Pattern.compile("(?&lt;!\\#)^\\#(\\b)*");
  h2 = Pattern.compile("(?&lt;!\\#)^\\#{2}(\\b)*");
  h3 = Pattern.compile("(?&lt;!\\#)^\\#{3}(\\b)*");
  h4 = Pattern.compile("(?&lt;!\\#)^\\#{4}(\\b)*");
  h5 = Pattern.compile("(?&lt;!\\#)^\\#{5}(\\b)*");
  h6 = Pattern.compile("(?&lt;!\\#)^\\#{6}(\\b)*");
  </code></pre>
  
  <p>Whenever I use \\#, my compiler (IntelliJ) tells me: "Redundant character escape". It does that whenever I use \\#. As far as I know, # should not be a special character in regex, so escaping it with two backslashes should allow me to use it. </p>
  
  <p>When I find a match, I want to surrond the entire match with bold HTML-tags, like this: "<b>###Heading</b>", but for some reason it's not working</p>
  
  <pre><code>//check for heading 6
  Matcher match = h6.matcher(tmp);
  StringBuffer sb = new StringBuffer();
  while (match.find()) {
      match.appendReplacement(sb, "&lt;b&gt;" + match.group(0) + "&lt;/b&gt;");
  }
  match.appendTail(sb);
  tmp = sb.toString();
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>So I have to seperately look at each heading, I can't look at heading 1-6 in the same pattern (this has to do with other parts of my program that uses the same pattern). What I know so far:</p>
  
  <ul>
  <li>If there is a heading in the string, it is at the start.</li>
  <li>If it starts with a heading, the entire string that follows is considered a heading, until the user presses Enter.</li>
  <li>If I have "## This a heading", then it must match true for h2, but false for h1. </li>
  <li>When I find my match, this "## This a heading" becomes this "<b>## This a heading</b>.</li>
  </ul>