Stumped over this one

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
terrypin
Posts: 172
Joined: Wed Jul 11, 2007 7:50 am

Stumped over this one

Post by terrypin »

I've been trying various regex expressions but so far in vain.

How do I change lines like this:
/*Set volume to 20*/

to this:
// Set volume to 20

IOW replace '/*' by '// ' (note the space), and delete '*/'
ben_josephs
Posts: 2457
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Find what: ^/\*(.*)\*/
Replace with: // $1
terrypin
Posts: 172
Joined: Wed Jul 11, 2007 7:50 am

Post by terrypin »

Thanks! Saw your impressively fast reply as I was coming back to proudly present the solution I came up with:

Code: Select all

Find what: (/*)(.*)(*/)
  Replace with: // 2
Seemed to work on first tests. Does that look reliable?
ben_josephs
Posts: 2457
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

There's a bug in the current version of the software that runs these forums. Single backslashes in posts get swallowed, so you have to double them. Your example should look like this:

Code: Select all

Find what: (/\*)(.*)(\*/)
Replace with: // \2
Yes, that works. But the first and third pairs of parentheses unnecessarily capture text that is discarded. Also, unlike the version I gave, it captures comments that aren't at the beginning of a line.

And in a TextPad replacement expression \2 means the same as $2 .
terrypin
Posts: 172
Joined: Wed Jul 11, 2007 7:50 am

Post by terrypin »

Indeed, quite a bug for discussions about regex! Particularly cases like this. Never spotted that before, thanks.

In fact my target strings can also occur anywhere in a line. I'll remember to add that spec here in future.
ben_josephs
Posts: 2457
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

If the original comments can occur anywhere in a line, perhaps followed by further text on the same line, then the replacement will change

Code: Select all

/* Unhelpful comment */  doEssentialThing () ;
to

Code: Select all

//  Unhelpful comment   doEssentialThing () ;
which is plainly wrong.

What spec?
User avatar
AmigoJack
Posts: 490
Joined: Sun Oct 30, 2016 4:28 pm
Location: グリーン ヒル ゾーン
Contact:

Post by AmigoJack »

How about lines that have multiple comments? Such as:

Code: Select all

if( /*bOne and bTwo and*/ bThree /*and bFour*/ ) {
Also all regex patterns also match empty comments and I just cannot imagine any code that ever makes use of such. Let alone the void sense of turning that into another comment:

Code: Select all

/**/
Post Reply