Kod içeren bazı metin dosyam var.Tüm yorumları program aracılığıyla silmek için Java regex
/*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
}
Ve öyle gibi gözüküyor olun: dosyayı okumak ve hepsini
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();
}
yerine Ama
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;
}
Yöntemi:
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();
}
ben bu denedim çalışmıyor (yorum silinmedi)
Tek bir normal ifade için bunun çok olduğunu söyleyeyim. Kodu bir çözümleyici ayrıştırıcısıyla ayrıştırmayı ve onunla yorumları bulmayı denemelisiniz. – SomeJavaGuy
['" //.*[\r\n]*|(\"[^\\\\\"]*(?:\\\\.[^\\\\\"]* deneyebilirsiniz) * \ "?) |/\\ * [^ *] * \\ * + ([^/*] [^ *] * \\ * +) * /" '] (https://regex101.com/r/yU4aU5/1). –