Skip to content

Instantly share code, notes, and snippets.

@natebass
Last active July 1, 2021 00:05
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save natebass/90b5ca184ac922f5d853 to your computer and use it in GitHub Desktop.
Save natebass/90b5ca184ac922f5d853 to your computer and use it in GitHub Desktop.
Remove comments from file. Credit to Lambda's answer on Stack Overflow https://stackoverflow.com/a/2613945/5178499
Remove all block comments and line comments:
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/|[ \t]*//.*)
Remove block comments:
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)
Remove line comments:
([ \t]*//.*)
However, I should warn that this works only %99.99 of time. You might have a string variable defined in your file like:
String myStr = "/** I am not a comment */";
This regex will turn this to:
String myStr = "";
@paray666
Copy link

Nice, thanks!

@matteusbarbosa
Copy link

Awesome, mate. Works like a charm. Just regex replace and... Voiláa. No need for specific software or anything else. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment