Link to home
Start Free TrialLog in
Avatar of davev
davev

asked on

Pattern Matching

I'm trying to develop a pattern to search through all my scripts for a particular subroutine call that does NOT have a particular paramter. For example, I'm looking for "dbConnect" where "fileName" is not a paramter between the parentheses. Such calls may span multiple lines. I'm having difficulty coming up with the pattern that will match:

dbConnect ();
dbConnect('user');
dbConnect ('user',
           line);

but not match:

dbConnect('user', fileName);
dbConnect ('user',
           line,
           fileName);

because "fileName" exists in these. Any help on this is greatly appreciated. Thanks.
Avatar of rj2
rj2

#!/usr/bin/perl
use strict;

my $match =<<END;
dbConnect ('user',
          line);
END

my $dontmatch=<<END;
dbConnect ('user',
          line,
          fileName);
END

test($match);
test($dontmatch);

sub test {
     my($s) = $_[0];
     if($s =~ /dbConnect\s*\(.*?[^f][^i][^l][^e][^N][^a][^m][^e].*?\)/gm) {
          print "Matched $s\n";
     } else {
          print "Did not match $s\n";
     }
}    
         
     
Avatar of davev

ASKER

That code behaves correctly as is, but if:

my $match = "dbConnect ('user', line);";
my $dontmatch = "dbConnect ('user', line, fileName);";

where the string is all on one line, then they both say match, but one obviously shouldn't. I have no way of knowing whether the dbConnect call is going to span multiple lines or not.
ASKER CERTIFIED SOLUTION
Avatar of rj2
rj2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial