# 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;