Skip to main content
deleted 569 characters in body
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29
sed  -E '/^\s*\/\*/!bx ; /\*\/\s*$/!bx ; /\*\/[^;]+;\s*\S+.*\/\*/bx ; d;  :x' draft 

If the comment line contains a (second)clsing comment endmarker followed by statement ;at least one non-whitespace character and eventually bythen another comment start then there is code inside so branch past the delete to x

/\*\/[^;]+;\s*\S+.*\/\*/bx

Testing for a valid code construct (????; followed by /*) means that this will reject malformed syntax such as

/* between two comments */ x = 0;  could be some malformed code */

To keep any text between comments that may have been 'meant to be code' then this section of the regex would be changed to look for at least one character before the next comment is opened

/\*\/.+\/\*/bx

Both of the above rely on the comments being well formed.

Since we found no valid code then delete

/* Comment 1 */
printf("It is /* Comment 2 */\n");
x = 5; /* Comment 3 */
            /* Comment 4 */
/* Comment 5 */ y = 0;
            /*
             * Comment 6
             */
            // Comment 7
/* between two comments */ x = 0;  /*could be some code */
/* between two comments */ x = 0;  some malformed*some code */
printf("It is /* Comment 2 */\n");
x = 5; /* Comment 3 */
/* Comment 5 */ y = 0;
            /*
             * Comment 6
             */
            // Comment 7
/* between two comments */ x = 0;  /*could be some*some code */
sed  -E '/^\s*\/\*/!bx ; /\*\/\s*$/!bx ; /\*\/[^;]+;.*\/\*/bx ; d;  :x'

If the comment line contains a (second) comment end followed by statement ; and eventually by another comment start then there is code inside so branch past the delete to x

/\*\/[^;]+;.*\/\*/bx

Testing for a valid code construct (????; followed by /*) means that this will reject malformed syntax such as

/* between two comments */ x = 0;  could be some malformed code */

To keep any text between comments that may have been 'meant to be code' then this section of the regex would be changed to look for at least one character before the next comment is opened

/\*\/.+\/\*/bx

Both of the above rely on the comments being well formed.

Since we found no valid code then delete

/* Comment 1 */
printf("It is /* Comment 2 */\n");
x = 5; /* Comment 3 */
            /* Comment 4 */
/* Comment 5 */ y = 0;
            /*
             * Comment 6
             */
            // Comment 7
/* between two comments */ x = 0;  /*could be some code */
/* between two comments */ x = 0;  some malformed code */
printf("It is /* Comment 2 */\n");
x = 5; /* Comment 3 */
/* Comment 5 */ y = 0;
            /*
             * Comment 6
             */
            // Comment 7
/* between two comments */ x = 0;  /*could be some code */
sed  -E '/^\s*\/\*/!bx ; /\*\/\s*$/!bx ; /\*\/\s*\S+.*\/\*/bx ; d;  :x' draft 

If the comment line contains a clsing comment marker followed by at least one non-whitespace character and then another comment start then there is code inside so branch past the delete to x

/\*\/\s*\S+.*\/\*/bx

Since we found no valid code then delete

/* Comment 1 */
printf("It is /* Comment 2 */\n");
x = 5; /* Comment 3 */
            /* Comment 4 */
/* Comment 5 */ y = 0;
            /*
             * Comment 6
             */
            // Comment 7
/* between two comments */ x = 0;  /*some code */
printf("It is /* Comment 2 */\n");
x = 5; /* Comment 3 */
/* Comment 5 */ y = 0;
            /*
             * Comment 6
             */
            // Comment 7
/* between two comments */ x = 0; /*some code */
correction to regex
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29
sed  -E '/^\s*\/\*/!bx ; /\*\/\s*$/!bx ; /\*\/[^;]+[^;]+;.*\/\*/bx ; d;  :x'
/\*\/[^;]+[^;]+;.*\/\*/bx

Testing for a valid code construct (????; followed by /*) means that this will reject malformed syntax such as

Both of the above rely on the comments being well formed.

sed  -E '/^\s*\/\*/!bx ; /\*\/\s*$/!bx ; /\*\/[^;]+.*\/\*/bx ; d;  :x'
/\*\/[^;]+.*\/\*/bx

Testing for a valid code construct (; followed by /*) means that this will reject malformed syntax such as

sed  -E '/^\s*\/\*/!bx ; /\*\/\s*$/!bx ; /\*\/[^;]+;.*\/\*/bx ; d;  :x'
/\*\/[^;]+;.*\/\*/bx

Testing for a valid code construct (????; followed by /*) means that this will reject malformed syntax such as

Both of the above rely on the comments being well formed.

In response to @Philippos comment
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29

To keep any text between comments that may have been 'meant to be code' then this section of the regex would be changed to look for at least one character before the next comment is opened

/\*\/.+\/\*/bx

Since we found no valid code then delete

Since we found no valid code then delete

To keep any text between comments that may have been 'meant to be code' then this section of the regex would be changed to look for at least one character before the next comment is opened

/\*\/.+\/\*/bx

Since we found no valid code then delete

added case for malformed code
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29
Loading
error in the closing regex
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29
Loading
error in the closing regex
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29
Loading
added 75 characters in body
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29
Loading
sample added
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29
Loading
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29
Loading