Regular expression query !!should be a good one (185 Views)
Reply
Advisor
Deepak_5
Posts: 36
Registered: ‎04-17-2001
Message 1 of 11 (185 Views)
Accepted Solution

Regular expression query !!should be a good one

Hi,

I have a definition in my file

int func(/* sdsd*/int a, /*deep*/char b);

I need to change it to
int func( int a, char b);
or
int func(int a,char b);

Basically, I need to get rid of the comments in the line.

Is there a regular expression substitution for this.
Also Note in "vi" I tried:
:%s/\/\*.*\*\///g

I tired may other patterns with "(" and ")" stuff, but somehow did not work.

Got any idea, please let me know.

Thanks and Regards
Deepak
Honored Contributor
Volker Borowski
Posts: 1,521
Registered: ‎04-20-1998
Message 2 of 11 (185 Views)

Re: Regular expression query !!should be a good one

Deepaq,

you just provided one DOT. So only any comment consisting of a single char will be replaced.

I am not so deep in regexps, but I would try

%s/\/\*.\*\///g
%s/\/\*..\*\///g
%s/\/\*...\*\///g
and so on (yes, not very sophisticated)...

Volker
Honored Contributor
Robin Wakefield
Posts: 861
Registered: ‎06-14-1998
Message 3 of 11 (185 Views)

Re: Regular expression query !!should be a good one

%s/\/\*[^\*]*\*\///g

should do it.

Robin
Honored Contributor
Thierry Poels_1
Posts: 1,218
Registered: ‎08-15-2000
Message 4 of 11 (185 Views)

Re: Regular expression query !!should be a good one

Hi,

:%s;/\*[^\*]*\*/;;g
or
:%g;func;s;/\*[^\*]*\*/;;g (for only the lines containing "func")

should work too; by replacing the seperator by ";" you don't need to escape the "/" in your search.

BTW: s;/\*.*\*/;... will replace everyting between the first "/*" and the last "*/" on a line !!

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Advisor
Deepak_5
Posts: 36
Registered: ‎04-17-2001
Message 5 of 11 (185 Views)

Re: Regular expression query !!should be a good one

Hi ,

The seems to be some problem, my commets cannot have '*' or '/'
e.g.
int func1( /* *sdf */ int *a ,/* */int b);
This is a valid C statement.

Can Anyone handle this as well ?

Thanks and Regards
Deepak
Honored Contributor
Thierry Poels_1
Posts: 1,218
Registered: ‎08-15-2000
Message 6 of 11 (185 Views)

Re: Regular expression query !!should be a good one

hmmz,
:%s;/\*[^/]*\*/;;g
will allow "*" in your comments, but no "/" :)
So if you run both substitutes, everything should be OK.

:%s;/\*[a-bA-Z0-9*/.:\; ]*\*/;;g
will do the trick in one step, add any character you need between the square brackets.

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Advisor
Deepak_5
Posts: 36
Registered: ‎04-17-2001
Message 7 of 11 (185 Views)

Re: Regular expression query !!should be a good one

Thanks Theiry,but
:%s;/\*[a-bA-Z0-9*/.:\; ]*\*/;;g

did not work fine for me .
e.g. int func1( /* sdf / *ddd */ int *a ,int b);
is the input.

There has to be way..

Thanks and Regards
Deepak


Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 8 of 11 (185 Views)

Re: Regular expression query !!should be a good one

Most regular expressions are greedy (matching as much as possible). Perl5 has an option to a reg expr to match a minimum. So try the following-

perl -p -i -e 's{/\*.*?\*/}{}' yourprog.c

The .*? will only match until the FIRST */ is found.
There be dragons...
Valued Contributor
David Totsch
Posts: 44
Registered: ‎02-07-1999
Message 9 of 11 (185 Views)

Re: Regular expression query !!should be a good one

Deepak:

try this with sed(1):

/func/ s#\(^.*\)\(\/\*..*\*\/\)\(.*\)\(\/\*..*\*\/\)\(.*$\)#\1 \3 \5#g

\(^.*\) starts matching the front
of input lines
\(\/\*..*\*\/\) matches the comment you
want to discard (at least
one character e.g. "/* */")
\(.*\) captures the text between
comments
\(\/\*..*\*\/\) matches the second comment
WARNING: _requires_ second cmnt
\(.*$\) captures text up to the end
of the line

\1 \3 \5 recalls what was matched by
those parenthetical statements
numbered starting with 1 from
left to right

Again, this statement will "hit" only on lines
that have two embedded comments.

Enjoy,
-dlt-
Honored Contributor
someone_4
Posts: 1,563
Registered: ‎05-16-2000
Message 10 of 11 (185 Views)

Re: Regular expression query !!should be a good one

If anyone is still reading this post.
Can someone tell me what you are talking about?

thanks

Richard
Advisor
Deepak_5
Posts: 36
Registered: ‎04-17-2001
Message 11 of 11 (185 Views)

Re: Regular expression query !!should be a good one

Richard,

We still are very much there to get a perfect solution.

The input is this line
int func1( /* sdf * */ int *a ,/* / */ int b/* wewe */);

Reduce it to.
int func1( int *a ,int b);

Note:
The may be any number of comments in a line , and any valid
character in those comments.

Thanks for the all the responses.
With response, we have got till now, there seems
to be no way through pure regular expression,
except for the perl stuff(Cannot use perl though).
Can be awk/sed.


Thanks and Regards
Deepak



The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.