Regex to replace C++ comment with C comment ?

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
sergeant
Posts: 7
Joined: Thu Sep 18, 2003 9:42 pm

Regex to replace C++ comment with C comment ?

Post by sergeant »

Hello,

is it possible to replace all C++ comments (//) within a C-sourcecode with C comments (/* ... */) using Textpad regex?

(I'm using Textpad 5.2)

Thanks for any hint.

sergeant
gan

Post by gan »

Yes you could search for:

Code: Select all

//(.*)
and replace with:

Code: Select all

/*\1*/
But since the /*....*/ can span several lines this might not be the result you want.

Let's say you have the following lines:

Code: Select all

//comment 1
//comment 2
//comment 3
HKEY   hk; //comment 4
Using the regex example above it will be like this:

Code: Select all

/*comment 1*/
/*comment 2*/
/*comment 3*/
HKEY   hk; /*comment 3*/
The example given above there are 3 lines in a row which is comments only and then it would make more sense to start with /* before comment 1 and end with */ after comment 3 instead of starting and ending the comment for each line. Using regex i cannot see how this could be done though since the // is for a single line only.

I guess it would be better if regex could change it into this, but i don't think that is possible:

Code: Select all

/*comment 1
comment 2
comment 3*/
HKEY   hk; /*comment 3*/
Post Reply