Skip to content

Instantly share code, notes, and snippets.

@rana01645
Forked from natebass/remove_comment_regex.txt
Created December 13, 2020 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rana01645/04eaae346d806058101b77755303c8f5 to your computer and use it in GitHub Desktop.
Save rana01645/04eaae346d806058101b77755303c8f5 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 = "";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment