2014-06-20 40 views
13

Ho molti commenti come seguire. C'è un modo semplice per rimuovere tutti i commenti?Rimuovi tutti i commenti in file Java

IDE Eclipse Keplero

/* 34: */ 

/* 

* JD-Core Version: 0.7.0.1 

*/ 
+3

Cosa succede se ho i file 10K Pensi ancora che in questo modo è semplice? –

+2

@shekharsuman: davvero? :) –

+0

https://forums.bukkit.org/threads/how-to-make-eclipse-clean-up-format-your-code-for-you.78574/ è questo? – Chang

risposta

37

Ho trovato la soluzione Regular Expression with multiple lines search.

Ecco l'espressione regolare usata per trovare due tipi di commenti

\/\*([\S\s]+?)\*\/ e (?s)/\*.*?\*/

aprire il file .java con i commenti e aprire la finestra di ricerca. (Search -> File Search) e incollare uno dei regi- sopra ex e controllare la casella di spunta Regular expression sul lato destro. Ora puoi cercare e selezionare "Sostituisci tutto" per sostituirlo con niente digitato nella seconda casella.

Utilizzo dell'opzione di sostituzione con Ho pulito tutti i commenti dai file java.

+1

non funziona -.- – berserk

+0

Funziona quando utilizzo Notepad ++ per me. –

+0

Non funziona in eclissi :( – berserk

0

penso eclissi supporta la ricerca regex e sostituire. mi piacerebbe provare qualcosa di simile:

search: (?s)(?>\/\*(?>(?:(?>[^*]+)|\*(?!\/))*)\*\/) 
replace all with no-space-character or nothing literally 

anche in relazione al tema: Eclipse, regular expression search and replace

Ho modificato la regex e testato: http://regex101.com/r/sU4vI2 Non sono sicuro se funziona nel vostro caso.

+0

Penso che qualcosa non funzioni con l'espressione regolare –

+0

\/\ *. * \ * \/reqular expressşon trova tutti/* */commenti simili. Ma non trova il commento con newline tra/**/ –

+0

Ho modificato la mia risposta. Non sono assolutamente sicuro se questo funzionerà da quando sono passato da eclissi a intellJ qualche tempo fa. – TheWhiteLlama

0

C'è un plugin che lo fa in un solo clic. Questo commento/rimuove tutto System.Out.

Vedere this, this e this.

+2

Se tutto quello che hai intenzione di fare è fare riferimento a OP altrove, è meglio pubblicare i tuoi link in un commento. Le risposte dovrebbero essere in grado di reggersi da sole, indipendentemente dal fatto che un collegamento sia presente o meno. – awksp

0

Eclipse ha diverse scorciatoie per il commento/codice di decompressione.

Per singola riga di commento codice Java: Ctrl + / (Forward Slash) e

linea singola rimuovere il commento: Ctrl + \ (Backslash)

Per più righe di codice Java commento: Ctrl + Shift + / (Forwards Slash) e

multilinea scommentare: Ctrl +Maiusc +\ (Backslash)

Nota: per commenti su più righe, selezionare tutte le linee che si desidera lasciare un commento,/togliere i commenti.

anche Ctrl +Maiusc +L aprirà un elenco di tutte le principali scorciatoie per Eclipse.

+0

così come posso sostituire tutti i commenti tra/**/e dopo // –

+1

Per i commenti a riga singola basta spostare il cursore su quella linea (o se ci sono più righe che hanno commenti che iniziano con // quindi selezionare tutte le linee) e premere Ctrl + \ (Backslash), se questo non funziona, prova Ctrl +/(Forwardslash). Dipende dalla tua versione. Lo stesso vale per i commenti multilinea. Seleziona tutte le linee e premi Ctrl + Maiusc + \ (barra rovesciata) (o/(barra in avanti)). – Yasin

+0

Penso che manchi qualcosa di importante. Ho file 10K, non vuoi renderli tutti manualmente? :) –

3

Ho creato un open source library per questo scopo, è possibile rimuovere i commenti Java.

Supporta rimuovere o NON rimuovere TODO.

Supporta anche JavaScript, HTML, CSS, proprietà, commenti JSP e XML.

piccolo frammento di codice come usarlo:

public static void main(String[] args) throws CommentRemoverException { 

// root dir is: /Users/user/Projects/MyProject 
// example for startInternalPath 

CommentRemover commentRemover = new CommentRemover.CommentRemoverBuilder() 
     .removeJava(true) // Remove Java file Comments.... 
     .removeJavaScript(true) // Remove JavaScript file Comments.... 
     .removeJSP(true) // etc.. goes like that 
     .removeTodos(false) // Do Not Touch Todos (leave them alone) 
     .removeSingleLines(true) // Remove single line type comments 
     .removeMultiLines(true) // Remove multiple type comments 
     .startInternalPath("src.main.app") // Starts from {rootDir}/src/main/app , leave it empty string when you want to start from root dir 
     .setExcludePackages(new String[]{"src.main.java.app.pattern"}) // Refers to {rootDir}/src/main/java/app/pattern and skips this directory 
     .build(); 

CommentProcessor commentProcessor = new CommentProcessor(commentRemover); 
        commentProcessor.start();   
    } 
3

Poiché nessuno ha menzionato strumenti di elaborazione del testo, grepsed, awk, ecc in * NIX, vi posto la mia soluzione qui.

Se il sistema operativo è * NIX, è possibile utilizzare lo strumento di elaborazione testo sed per rimuovere i commenti.

sed '/\/\*/{:loop;/\/\*.*\*\//{d;b out};N;b loop};:out' yourfile.java 
+1

domanda specifica in particolare come farlo in Eclipse, ma se qualcuno raggiunge questo Q & A cercando ** qualsiasi ** modo di rimuovere commenti, la tua risposta sarà utile, quindi trascurando l'applicabilità nel rispondere alla Q sopra, +1 – Mindwin

0

Si potrebbe provare a utilizzare uncrustify (http://uncrustify.sourceforge.net/) per riformattare il /* block comments */ a // double-slash comments

che renderebbe il vostro regex un po ' "più sicuro" - basta guardare per \*s// linee ed eliminarli (facile sed funzionamento)

1

Questo sta lavorando bene per me ... ho aggiunto come un modello di ricerca IntelliJ IDEA per JavaDoc e/o una linea singola commenti ..

(?sm)(^(?:\s*)?((?:/\*(?:\*)?).*?(?<=\*/))|(?://).*?(?<=$)) 

Definizione


Options:^and $ match at line breaks 

    Match the remainder of the regex with the options: dot matches newline (s);^and $ match at line breaks (m) «(?sm)» 
    Match the regular expression below and capture its match into backreference number 1 «(^(?:\s*)?((?:/\*(?:\*)?).*?(?<=\*/))|(?://).*?(?<=$))» 
     Match either the regular expression below (attempting the next alternative only if this one fails) «^(?:\s*)?((?:/\*(?:\*)?).*?(?<=\*/))» 
      Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
      Match the regular expression below «(?:\s*)?» 
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?» 
      Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s*» 
       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
      Match the regular expression below and capture its match into backreference number 2 «((?:/\*(?:\*)?).*?(?<=\*/))» 
      Match the regular expression below «(?:/\*(?:\*)?)» 
       Match the character “/” literally «/» 
       Match the character “*” literally «\*» 
       Match the regular expression below «(?:\*)?» 
        Between zero and one times, as many times as possible, giving back as needed (greedy) «?» 
        Match the character “*” literally «\*» 
      Match any single character «.*?» 
       Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» 
      Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=\*/)» 
       Match the character “*” literally «\*» 
       Match the character “/” literally «/» 
     Or match regular expression number 2 below (the entire group fails if this one fails to match) «(?://).*?(?<=$)» 
      Match the regular expression below «(?://)» 
      Match the characters “//” literally «//» 
      Match any single character «.*?» 
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» 
      Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=$)» 
      Assert position at the end of a line (at the end of the string or before a line break character) «$» 
+0

L'unico inconveniente è che se hai una linea con una stringa come '.." http://www.domain.com ".. anche questo lo prenderà anche tu – IvanRF

+0

Ya ... non è perfetto ... Non ho provato molto uso casi ... Lo uso caso per caso. –

+0

Qualcosa come '([^:] //). *? (? <= $)' potrebbe evitare questi casi – IvanRF

0

ho fatto questo con il comando Ctrl+F e questo regex.

Questa sostituire solo //comments

//(.?+)+ 

enter image description here

Problemi correlati