Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Regular expression query !!should be a good one

SOLVED
Go to Solution
Highlighted
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
Honored Contributor [Founder]

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 [Founder]

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

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

should do it.

Robin
Honored Contributor [Founder]

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

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 [Founder]

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

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 [Founder]

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 [Founder]

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 [Founder]

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

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