in reply to Capture uncommented keywords

Is there another way?

Remove comments from the input before matching.

For trivial code containing only // comments (not /* */) and without string constants containing //, the following should do the trick:

#!/usr/bin/perl use strict; use warnings; while (<>) { chomp; my $orig=$_; s|//.*||; # strip comments if (/\b(printf|scanf|open|close|read|write)\b/) { print "Found keyword '$1' in '$_'.\nOriginal line: '$orig'\n"; } }

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: Capture uncommented keywords
by ExReg (Priest) on Jul 28, 2015 at 18:23 UTC
    The more I think about the problem, the more it looks like I will have to do additional processing like creating a copy of the code with the commented lines removed. It will add additional cycles to the analysis and make it simply blazingly fast instead of insanely fast compared to the old. Sigh...

    Thanks