title: Syntax-Highlighting for Java-Code in html
body: <p><strong>What I am trying to do</strong></p>
  <p>I am working on a syntax-highlighter that highlights java-code in html (example below).</p>
  <p><strong>Example output</strong></p>
  <p>I've tested the code with the following java-code (caesar-encryption):</p>
  <pre><code>//Encryption
  public static String encrypt(String text, int n) {
      int x = 0;
      int y = 0;
      String out = &quot;&quot;; //Empty string for result.
      while (x &lt; text.length()) {
          if (text.charAt(x) &gt; 64 &amp;&amp; text.charAt(x) &lt; 91) {
              if (text.charAt(x) + n &gt; 90) {
                  y = 26;
              }
              out = out + (char) (text.charAt(x) + n - y);
          } else {
              out = out + text.charAt(x);
          }
          x++;
          y = 0;
      }
      return out;
  }
  </code></pre>
  <p>The output looks like this:</p>
  <pre class="lang-html prettyprint-override"><code>&lt;!-- Code begins here --&gt;
  &lt;div style = &quot;background: LightGray; font: monospace; width: fit-content; height: min-height;&quot;&gt;&lt;pre&gt;&lt;code&gt;
  &lt;span style='color:green; font-weight:bold; '&gt;//Encryption
  &lt;/span&gt;&lt;span style='color:#7f0055; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#7f0055; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#7f0055; font-weight:bold; '&gt;String&lt;/span&gt; encrypt(&lt;span style='color:#7f0055; font-weight:bold; '&gt;String&lt;/span&gt; text, &lt;span style='color:#7f0055; font-weight:bold; '&gt;int&lt;/span&gt; n) {
      &lt;span style='color:#7f0055; font-weight:bold; '&gt;int&lt;/span&gt; x = 0;
      &lt;span style='color:#7f0055; font-weight:bold; '&gt;int&lt;/span&gt; y = 0;
      &lt;span style='color:#7f0055; font-weight:bold; '&gt;String&lt;/span&gt; out = &lt;span style='color:blue; font-weight:bold; '&gt;&quot;&quot;&lt;/span&gt;; &lt;span style='color:green; font-weight:bold; '&gt;//Empty string &lt;span style='color:#7f0055; font-weight:bold; '&gt;for&lt;/span&gt; result.
  &lt;/span&gt;    &lt;span style='color:#7f0055; font-weight:bold; '&gt;while&lt;/span&gt; (x &lt; text.length()) {
          &lt;span style='color:#7f0055; font-weight:bold; '&gt;if&lt;/span&gt; (text.charAt(x) &gt; 64 &amp;&amp; text.charAt(x) &lt; 91) {
              &lt;span style='color:#7f0055; font-weight:bold; '&gt;if&lt;/span&gt; (text.charAt(x) + n &gt; 90) {
                  y = 26;
              }
              out = out + (&lt;span style='color:#7f0055; font-weight:bold; '&gt;char&lt;/span&gt;) (text.charAt(x) + n - y);
          } &lt;span style='color:#7f0055; font-weight:bold; '&gt;else&lt;/span&gt; {
              out = out + text.charAt(x);
          }
          x++;
          y = 0;
      }
      &lt;span style='color:#7f0055; font-weight:bold; '&gt;return&lt;/span&gt; out;
  }
  &lt;/pre&gt;&lt;/code&gt;&lt;/div&gt;
  &lt;!-- Code ends here --&gt;
  </code></pre>
  <p>If you open this code with a browser, it looks like this:</p>
  <p><a href="https://i.stack.imgur.com/pTWFE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pTWFE.png" alt="enter image description here" /></a></p>
  <p><strong>The code</strong></p>
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.FlowLayout;
  import java.awt.Font;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  import javax.swing.ScrollPaneConstants;
  
  public class Gui {
  
    private JTextArea inputArea;
    private JTextArea outputArea;
    private boolean inputAreaClicked = false;
  
    public Gui() {
      //JFrame
      JFrame frame = new JFrame(&quot;Syntax-Highlighter&quot;);
      frame.setSize(700, 700);
      frame.setMinimumSize(new Dimension(700, 700));
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      
  
      //JPanel
      JPanel panel = new JPanel(new GridBagLayout());
      
      //Creating textArea for input
      inputArea = new JTextArea(&quot;Enter text here...&quot;);
      inputArea.setLineWrap(true);
      inputArea.setTabSize(1);
      inputArea.setFont(new Font(&quot;monospaced&quot;, Font.ITALIC, 12));
      inputArea.addMouseListener(new MouseAdapter() {     //Set text to &quot;&quot; the first time the inputArea is clicked
        @Override
        public void mouseClicked(MouseEvent e){
          if(!inputAreaClicked) {
            inputArea.setText(&quot;&quot;);
            inputArea.setFont(new Font(&quot;monospaced&quot;, Font.PLAIN, 12));
            inputAreaClicked = true;
          }
        }
      });
      JScrollPane input = new JScrollPane(inputArea);
      input.setPreferredSize(new Dimension(600, 110));
      input.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
      input.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  
      //Creating buttons
      
      JPanel buttonPanel = new JPanel(new FlowLayout(0, 50, 10)); //FlowLayout(align, hgap, vgap)
      JButton button = new JButton(&quot;Highlight&quot;);
      button.setFont(new Font(&quot;Arial&quot;, Font.PLAIN, 20));
      button.addActionListener(e -&gt; hightlight(inputArea.getText()));
      button.setPreferredSize(new Dimension(140,30));
     
      //Creating textArea for output
      outputArea = new JTextArea();
      outputArea.setEditable(false);
      outputArea.setTabSize(1);
      outputArea.setLineWrap(false);
      outputArea.setFont(new Font(&quot;monospaced&quot;, Font.PLAIN, 12));
      JScrollPane output = new JScrollPane(outputArea);
      output.setPreferredSize(new Dimension(600, 400));
      output.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
      output.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  
      //Layout
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.weighty = 5;
      gbc.gridwidth = GridBagConstraints.REMAINDER;
  
      //Add everything to panel
      buttonPanel.add(button);
      
      panel.add(input, gbc);
      panel.add(buttonPanel, gbc);
      panel.add(output, gbc);
      
      //Add everything to frame
      frame.add(panel, BorderLayout.CENTER);
      frame.setVisible(true);
    }
  
    private void hightlight(String text) {
      //Keywords
      String[] array = {
          &quot;abstract&quot;, &quot;assert&quot;, &quot;boolean&quot;, &quot;break&quot;, &quot;byte&quot;, &quot;case&quot;, 
          &quot;catch&quot;, &quot;char&quot;, &quot;class&quot;, &quot;const&quot;, &quot;continue&quot;, &quot;default&quot;, &quot;do&quot;, 
          &quot;double&quot;, &quot;else&quot;, &quot;enum&quot;, &quot;extends&quot;, &quot;false&quot;, &quot;final&quot;, &quot;finally&quot;, &quot;float&quot;,
          &quot;for&quot;, &quot;goto&quot;, &quot;if&quot;, &quot;implements&quot;, &quot;import&quot;, &quot;instanceof&quot;, &quot;int&quot;, 
          &quot;interface&quot;, &quot;long&quot;, &quot;native&quot;, &quot;new&quot;, &quot;null&quot;, &quot;package&quot;, &quot;private&quot;, &quot;protected&quot;,
          &quot;public&quot;, &quot;return&quot;, &quot;short&quot;, &quot;static&quot;, &quot;String&quot;, &quot;strictfp&quot;, &quot;super&quot;, &quot;switch&quot;, &quot;synchronized&quot;, 
          &quot;System&quot;, &quot;this&quot;, &quot;throw&quot;, &quot;throws&quot;, &quot;transient&quot;, &quot;true&quot;, &quot;try&quot;, &quot;void&quot;, &quot;volatile&quot;, &quot;while&quot;
      };
      
      //Highlight every keyword with color:#7f0055
      for(int i = 0; i &lt; array.length; i++) {
        text = text.replaceAll(array[i] + &quot;(?![a-zA-Z])&quot;, &quot;&lt;span style='color:#7f0055; font-weight:bold; '&gt;&quot; + array[i] + &quot;&lt;/span&gt;&quot;);
  
      }
      //Highlight Strings
      text = text.replaceAll(&quot;\&quot;(?&lt;render&gt;.*?)\&quot;&quot;, &quot;&lt;span style='color:blue; font-weight:bold; '&gt;&quot; + &quot;\&quot;${render}\&quot;&quot; + &quot;&lt;/span&gt;&quot;);
      
      //Highlight import-statements
      text = text.replaceAll(&quot;import(?&lt;render&gt;.*?);&quot;, &quot;&lt;span style='color:#7f0055; font-weight:bold; '&gt;&quot; + &quot;import${render};&quot; + &quot;&lt;/span&gt;&quot;);
      
      //Highlight multiline-comments
      Pattern p = Pattern.compile(&quot;/\\*(?&lt;render&gt;.*?)\\*/&quot;, Pattern.DOTALL);
      Matcher m = p.matcher(text);
      text = m.replaceAll(&quot;&lt;span style='color:green; font-weight:bold; '&gt;&quot; + &quot;/\\*${render}\\*/&quot; + &quot;&lt;/span&gt;&quot;);
      
      //Highlight single-line-comments
      text = text.replaceAll(&quot;//(?&lt;render&gt;.*?)\n&quot;, &quot;&lt;span style='color:green; font-weight:bold; '&gt;&quot; + &quot;//${render}\n&quot; + &quot;&lt;/span&gt;&quot;);
      
      text = &quot;&lt;!-- Code begins here --&gt;\n&lt;div style = \&quot;background: LightGray; font: monospace; width: fit-content; height: min-height;\&quot;&gt;&lt;pre&gt;&lt;code&gt;\n&quot; 
          + text + &quot;\n&lt;/pre&gt;&lt;/code&gt;&lt;/div&gt;\n&lt;!-- Code ends here --&gt;\n&quot;;
      outputArea.setText(text);
    }
  }
  </code></pre>
  <p><strong>The problem</strong></p>
  <p>As you can see in the picture above, keywords in comments are still highlighted as keywords.</p>
  <p>Do you have an idea how this problem can be solved?</p>
  <p><strong>What I have tried</strong></p>
  <p>I tried to use the following code (replacing in the backreference), but it didn't work:</p>
  <pre><code>// Highlight single-line-comments
  text = text.replaceAll(&quot;//(?&lt;render&gt;.*?)\n&quot;, &quot;&lt;span style='color:green; font-weight:bold; '&gt;&quot;
          + &quot;//&quot; + &quot;${render}&quot;.replaceAll(&quot;color: #7f0055&quot;, &quot;color: green&quot;) + &quot;\\n&lt;/span&gt;&quot;);
  </code></pre>
  <p>Unfortunately this isn't working? How can this be corrected?</p>