captain monk
captain monk

Reputation: 727

REGEX to remove multiline comments

i've searched quite a lot and the best i created till now to match multiline c comments is:

\/\*(.|\n)*\*\/

but for a text like this:

1. Not a comment
2.
3.Ooops Not a comment /**** A multiline comment **/  Ooops Not a comment
4. Ooops Not a comment /**** A multiline comment **\/
5.  ******  Ooops Not a comment
6.  ***/
7.// A another comment
8.Another not a comment
9.
10. "/*Again not a comment*/"
11.// A line comment at the end of file

The outcome looks like this: 

1. Not a comment
2.
3.Ooops Not a comment "
11.// A line comment at the end of file

Can someone explain my mistake and give me the correct one? thank you!

Full code: [spoiler]

%option noyywrap
%{
#include <stdio.h>
#include <stdlib.h>
FILE *fout;
%}

%%
\/\*(.|\n)*?\*\/
%%
void main( int argc, char ** argv)
{
    if ( argc < 3 )
    {
        printf("\nError!!! Missing Command line arguments");
        printf("\nUsage exe <inputfile> <outputfile>");
        exit(1);
    }
    else
    {
        fout = fopen(argv[2],"w");
        yyout = fout;
        yyin = fopen(argv[1],"r");
        yylex();
    }
    system("pause");
}

[/spoiler]

Upvotes: 0

Views: 646

Answers (1)

tenub
tenub

Reputation: 3446

Try adding a ? to make it non-greedy: \/\*(.|\n)*?\*\/.

Matches:

  1. /**** A multiline comment **/
  2. /**** A multiline comment **\/ 5. ****** Ooops Not a comment 6. ***/
  3. /*Again not a comment*/

If these matches are not correct please explain better what exactly should be matched.

Upvotes: 1

Related Questions