in reply to avoiding a particular character or set of characters
For that, I think you'll need to chunk through the data in the style of a parser, copying pieces to a separate string variable as you go, and modifying only the pieces that are uncommented public declarations. Something like this might be a start (untested):
Obviously, that will break under a variety of circumstances, e.g. if the java code happens to include "/*" or "*/" inside a quoted string. To do the job right, you would really need to use something like Parse::RecDescent, and implement a grammar that emulates a fair bit of what the java compiler does.# assume $src contains a whole file of java code... # first, split $src code into an array, on "/* ... */" style comments, # making sure to keep these comments as array elements my @chunks = split m{ (/\* .*? \*/) }sx, $src; # now go through the chunks, and look for uncommented "public" methods my $mod; # this will be the output string for ( @chunks ) { if ( m{^/\*} ) { # this is a "/*...*/" comment $mod .= $_; # so just copy it to output } else { my @lines = split /[\r\n]+/; for ( @lines ) { if ( m{^\s*public\s+\w+} ) { # this line looks like a public declaration # so do something special with it } $mod .= $_; # copy line to output } } } print $mod;
|
|---|