in reply to Matching C-Style comments

Hi,
If I'm not completely wrong it should work using
if ($file[$i] =~ m(/\*)) ...
This takes the matching operator m, uses parens as delimiter to the regexp
and only has to escape the *

Actually I tried it on the command line using

$ perl -p -e 'print "MATCH" if m(/\*)' foo foo /* MATCH/*
which seems to me like it works. Correct me if I'm getting things wrong. BTW: is there a better command line for this? I don't understand why the first line (which is what I type in) is echoed once

Regards... Stefan

Replies are listed 'Best First'.
Re: Matching C-Style comments
by Abigail (Deacon) on Jun 20, 2001 at 02:44 UTC
    Well, your regex isn't enough, because while it matches /*, it doesn't check whether that occurs inside a string or not - and when it's inside a string, it's not the start of the comment. I won't repeat the regex here, you can find it in the hip owls book, and there are pointers to it already posted.

    As for your second question, it's not just the first line that's echoed, it's the second line too, there's a /* after the MATCH! The reason is your use of -p, which means that $_ is printed for each line of input processed. Regardless of any print statements in the program. You probably want to use -n instead.

    -- Abigail