title: Java regex for deleting all comments programmatically
body: <p>I have some text file with code.</p>
  
  <pre><code> /*Comment here*/
  
   public void start(Stage primaryStage) throws Exception{
      Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
      primaryStage.setTitle("First");
  /*Comment here
  *and
  *here*/
      primaryStage.setScene(new Scene(root, 640, 480));
      primaryStage.show();//Comment this
  //and comment that
  }
  </code></pre>
  
  <p>And make it looks like that:</p>
  
  <pre><code> public void start(Stage primaryStage) throws Exception{
      Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
      primaryStage.setTitle("First");
      primaryStage.setScene(new Scene(root, 640, 480));
      primaryStage.show();
  }
  </code></pre>
  
  <p>i've tried this:</p>
  
  <pre><code> public String delComments(String content){
      Pattern regex = Pattern.compile("/\\*.*?\\*/|/{2,}[^\\n]*", Pattern.MULTILINE);
      Matcher matcher = regex.matcher(content);
      String clean = content.replaceAll("(?s:/\\*.*?\\*/)|//.*", "");
      return clean;
  }
  </code></pre>
  
  <p>Method that read file and replace it all</p>
  
  <pre><code>public void delCommentAction(ActionEvent actionEvent) throws IOException {
      String line = null;
      FileReader fileReader =
              new FileReader(filePath);
      BufferedReader bufferedReader =
              new BufferedReader(fileReader);
      FileWriter fw = new FileWriter(filePathNoComm);
      BufferedWriter bw = new BufferedWriter(fw);
      while((line = bufferedReader.readLine()) != null) {
          bw.write(delComments(line));
      }
      bw.close();
  }
  </code></pre>
  
  <p>But it doesn't work(comments weren't deleted)</p>