in reply to avoiding a particular character or set of characters

If I understand correctly, you want to make an edited version of some java code in which the "public" declarations are all modified somehow, but you want to leave any commented occurrences of such declarations unmodified. And you have the whole file of java code stored as a single scalar string.

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):

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