Operating System - HP-UX
1751914 Members
4921 Online
108783 Solutions
New Discussion юеВ

Regular expression query !!should be a good one

 
SOLVED
Go to solution
Deepak_5
Advisor

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
10 REPLIES 10
Volker Borowski
Honored Contributor

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
Robin Wakefield
Honored Contributor
Solution

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

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

should do it.

Robin
Thierry Poels_1
Honored Contributor

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.
Deepak_5
Advisor

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
Thierry Poels_1
Honored Contributor

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.
Deepak_5
Advisor

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


Rodney Hills
Honored Contributor

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...
David Totsch
Valued Contributor

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-
someone_4
Honored Contributor

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