Trong Posted July 1, 2016 Share Posted July 1, 2016 (edited) Hello, I need help to remove all comments in C++ source files. Source code with the Korean language in the comments. I want to remove it with AutoIt, is too long to remove by hand. Thanks. C++ comments start with /* and end with */. For example: /* This is a comment */ /* C++ comments can also * span multiple lines */ A comment can also start with //, extending to the end of the line. For example: #include <iostream> using namespace std; // Begin Apps main() { cout << "Hello World"; // prints Hello World return 0; } Within a /* and */ comment, // characters have no special meaning. Within a // comment, /* and */ have no special meaning. Thus, you can "nest" one kind of comment within the other kind. For example: /* Comment out printing of Hello World: cout << "Hello World"; // prints Hello World */ Edited July 1, 2016 by Trong Solved Regards, Link to comment Share on other sites More sharing options...
jguinch Posted July 1, 2016 Share Posted July 1, 2016 (edited) An easy way : Local $sCode = "/* This is a comment */" & @CRLF & _ "/* C++ comments can also" & @CRLF & _ " * span multiple lines" & @CRLF & _ " */" & @CRLF & _ "" & @CRLF & _ "A comment can also start with //, extending to the end of the line. For example:" & @CRLF & _ "" & @CRLF & _ "#include <iostream>" & @CRLF & _ "using namespace std;" & @CRLF & _ "" & @CRLF & _ "main()" & @CRLF & _ "{" & @CRLF & _ " cout << ""Hello World""; // prints Hello World" & @CRLF & _ "" & @CRLF & _ " return 0;" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "Within a /* and */ comment, // characters have no special meaning. Within a // comment, /* and */ have no special meaning. Thus, you can ""nest"" one kind of comment within the other kind. For example:" & @CRLF & _ "" & @CRLF & _ "/* Comment out printing of Hello World:" & @CRLF & _ "" & @CRLF & _ "cout << ""Hello World""; // prints Hello World" & @CRLF & _ "" & @CRLF & _ "*/" Local $sCodeWithoutComments = StringRegExpReplace($sCode, "(?s)(/\*.*?\*/|//\N*(?=\R|$))\R?", "") ConsoleWrite($sCodeWithoutComments) But with my code, you will have problems if a line of your code contains string with "//" on "/*" or "*/" Edited July 1, 2016 by jguinch Trong 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Trong Posted July 1, 2016 Author Share Posted July 1, 2016 Thanks jguinch, script worked Global $sCode = @CRLF $sCode &= "/* This is a comment */" & @CRLF $sCode &= "" & @CRLF $sCode &= "/* C++ comments can also" & @CRLF $sCode &= " * span multiple lines" & @CRLF $sCode &= " */" & @CRLF $sCode &= "#include <iostream>" & @CRLF $sCode &= "using namespace std;" & @CRLF $sCode &= "// Begin Apps" & @CRLF $sCode &= "main()" & @CRLF $sCode &= "{" & @CRLF $sCode &= ' cout << "Hello World"; // prints Hello World' & @CRLF $sCode &= "" & @CRLF $sCode &= " return 0;" & @CRLF $sCode &= "}" & @CRLF $sCode &= "/* Comment out printing of Hello World:" & @CRLF $sCode &= "" & @CRLF $sCode &= 'cout << "Hello World"; // prints Hello World' & @CRLF $sCode &= "" & @CRLF $sCode &= "*/" & @CRLF & @CRLF Global $sCodeWithoutComments = StringRegExpReplace($sCode, "(?s)(/\*.*?\*/|//\N*(?=\R|$))\R?", "") ConsoleWrite($sCodeWithoutComments) Regards, Link to comment Share on other sites More sharing options...
jguinch Posted July 1, 2016 Share Posted July 1, 2016 You're welcome Trong 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now