Here's a little snippet that produces the output you desire. However, it doesn't really take into account nested comments or multiline comments, but you can extend that on your own.
By the way, your last line of sample code /*The end!/*} is not a valid comment terminator. I think you meant /*The end!*/}.
#!/usr/bin/perl
use strict;
use warnings;
undef $/;
my $code = <DATA>;
# $code =~ s|^\s*/\*.*?\*/||gm;
# Changed as per anjiro's comments
$code =~ s#^\s*/\*.*?\*/##gm;
$code =~ s#(?<=\S)\s/\*.*?\*/\s*?$##gm;
$code =~ s#\n##ms;
print $code;
__DATA__
/*This is a bogus function*/
void function foo(void) /*My function is the best*/
{
int i; /*i is an integer*/
int j; /*j is also an integer*/
/*Now I'm going to set i to 1*/
i = 1;
/*Also j*/
j = 1;
/*Here's some incrementing!*/
i++;
/*And more!*/
j++;
/*The end!*/}
Hope this helped, -v.
"Perl. There is no substitute."
Update: Changed the regexes to reflect anjiro's comments.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|