neeha has asked for the wisdom of the Perl Monks concerning the following question:

Hi Experts,
I have some java code like
public void check1(); /** public void check2();*/
or it can be like
/** public void check2(); */ //public void check2();
I am converting sentences which start with public
my match is
if($line=m/public(*.);/)
how can i make sure that i get only methods which are not commented.
i tried giving [^/**] to test if it works but it does not work.
can u please help me

2006-03-28 Retitled by planetscape, as per Monastery guidelines
Original title: 'avoiding a particular characteror a set of characters'

Replies are listed 'Best First'.
Re: avoiding a particular character or set of characters
by ptum (Priest) on Mar 27, 2006 at 14:48 UTC

    You might try something more like this:

    if ($line =~ /^\s*public(*.);\s*$/) { # do something }

    The use of anchors (^ and $) at the beginning and end of your pattern will ensure that nothing (except some optional space characters) will appear before the 'public' or after the ';'. You may be able to adapt this to your needs.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: avoiding a particular character or set of characters
by lima1 (Curate) on Mar 27, 2006 at 15:04 UTC
    the easiest solution I see is filtering out those comments.
    while ( $javacode =~ s{ (/\*\* .*? \*/) }{ <!--REPLACED_COMMENT--> }xm +s ){ push @comments, $1; } my @lines = (); LINE: foreach my $line (split "\n", $javacode) { push @lines, $line; next LINE if $line =~ m { \A \s* // }xms; # change lines $lines[-1] =~ ... } $javacode = join "\n", @lines; my $comment_id = 0; $javacode =~ s{<!--REPLACED_COMMENT-->}{$comment[$comment_id++]}eg;
    I'm sure there is a shorter version and I want to see that, saints :)
      cant i have a solution like
      $str ="check this out";
      if this sentence has out do not replace it
      if($str =~ s#check this [^out]#check this one#){ }
      is there some way to achieve this
      one more thing i am not traversing line by line.
      the whole java file i am taking as a single line
      using undef $/;

        Hi neeha, you cannot use character class here, rather you have to use negative look head.

        change

        if($str =~ s#check this [^out]#check this one#){ }

        into

        $str ="$str ="check this out check this here";"; $str =~ s#check this ((?!out).)+#check this one#gs; print $str;

        Prasad

        my problem is
        /** * public void method1(); * is deprecated */ public void method1();
        in this case my code replaces to
        /** * replaced code */ public void method1();
        It dont want to replace anything in the comments
        and my code reads the entire java file as a single line.
Re: avoiding a particular character or set of characters
by CountOrlok (Friar) on Mar 27, 2006 at 14:58 UTC
    You can adapt the following to your needs (it does not cover all the ways one can put comments in code):
    my $inComment = 0; while(<>) { next if $inComment; s|//.*$||; # remove // comments s|/\*.*?\*/||; # remove /*..*/ comments s|^.*\*/|| and $inComment = 0; s|/\*.*$|| and $inComment = 1; # do your pattern matching on $_ here }
    -imran
Re: avoiding a particular character or set of characters
by graff (Chancellor) on Mar 28, 2006 at 08:16 UTC
    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.