Solved

Pattern Matching

Posted on 2002-05-28
3
398 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl, group, sort, count question 6 131
Removing file extension within a file. 4 92
perl: Cleaning meta tags using RegEX 12 75
html form to write data to csv 24 113
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

15 Experts available now in Live!

Get 1:1 Help Now