Jump to content

[Solved] Help regular expression to to remove all comments in C++ source files


Recommended Posts

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 by Trong
Solved

Regards,
 

Link to comment
Share on other sites

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 by jguinch
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...