Solved

Pattern Matching

Posted on 2002-05-28
3
397 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
ID: 7040021
#!/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
ID: 7040065
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
ID: 7040119
#!/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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

920 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