Solved

Pattern Matching

Posted on 2002-05-28
3
395 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:davev
  • 2
3 Comments
 
LVL 10

Expert Comment

by:rj2
Comment Utility
#!/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";
     }
}    
         
     
0
 

Author Comment

by:davev
Comment Utility
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.
0
 
LVL 10

Accepted Solution

by:
rj2 earned 50 total points
Comment Utility
#!/usr/bin/perl
use strict;

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


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

sub test {
     my($s) = $_[0];
     if($s =~ /(dbConnect\s*\((.|\s)*?\))/gm) {    
          my($s2)=$1;
          if($s2 =~ /fileName/) {
               print "Did not match $s\n";
          } else {
               print "Matched $s\n";
          }          
     } else {
          print "Did not match $s\n";
     }
}    
         
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now