Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pattern Matching

Posted on 2002-05-28
3
Medium Priority
?
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

598 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