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.

In reply to Re: avoiding a particular character or set of characters by graff
in thread avoiding a particular character or set of characters by neeha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.