By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,934 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,934 IT Pros & Developers. It's quick & easy.

regexp not working

P: 13
Hi

I'm new here. I'm trying to put some code to copy files with specific patterns/keywords from a target location. I encountered a pattern matching (regexp) issue where the filename didn't contain the keyword but got returned as matching. The script is looking for a list of PROD in the setup file and dynamically changing the keyword by replacing the PROD into the filename. The keyword will be updated every loop.

File: setup_demo.txt
==============================
Expand|Select|Wrap|Line Numbers
  1. [COPY]
  2. PROD=DATA,GRAPH
  3. filename=$PROD_GROUP2_*
  4. filepath=C:\temp
==============================

In this setup, I'm expecting the script to search for DATA_GROUP2_* in the 1st loop and then GRAPH_GROUP2_* during the 2nd loop. The code worked on the 1st loop but failed on the subsequent loop.

Output on screen shows:
==============================
Expand|Select|Wrap|Line Numbers
  1. INFO   : Searching File with Keyword [DATA_GROUP2_*]
  2. INFO   : Found Local File [DATA_GROUP2_20080720.zip] matches DATA_GROUP2_*
  3. ..
  4. ..
  5. INFO   : Searching File with Keyword [GRAPH_GROUP2_*]
  6. INFO   : Found Local File [DATA_GROUP2_20080720.zip] matches GRAPH_GROUP2_*
==============================

This should not happen as the keyword should not match the filename: Found Local File [DATA_GROUP2_20080720.zip] matches GRAPH_GROUP2_*

I trimmed the script so that the error can be re-produced.
==============================
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. # initialization
  5. my %setup     = ();
  6. my $section     = "";
  7. my $key     = "";
  8. my $val     = "";
  9. my $keyword    = ();
  10.  
  11. # read setup file
  12. print "INFO   : Reading setup file \[setup.txt\]\n";
  13. open(SETUP,"<setup_demo.txt") or die "Cannot open setup.txt\n";
  14. while(<SETUP>) {
  15.         chomp;
  16.     next if (/^\s*$/);
  17.     if (/\[(.*)\]/) {
  18.         $section = $1;
  19.         next;
  20.     }
  21.     ($key,$val) = split(/\=/);
  22.     $setup{$section}{$key} = $val;
  23. }
  24. close(SETUP);
  25.  
  26. my @PROD = split(',',$setup{'COPY'}{'PROD'});
  27. print "INFO   : Total of " . ($#PROD + 1) . " Product(s)\n";
  28. for my $product (@PROD) {
  29.     my @localfiles         = ();
  30.     my $localfile         = "";
  31.     my %varsetup         = ();
  32.     print "INFO   : Processing Product \[$product\]\n";
  33.  
  34.     # Reset setup into varsetup to populate the product
  35.     foreach $section (keys %setup) {
  36.         foreach $key (keys (%{$setup{$section}})) {
  37.             my $myval = $setup{$section}{$key};
  38.             $varsetup{$section}{$key} = $myval;
  39.         }
  40.     }
  41.     foreach $section (keys %varsetup) {
  42.         foreach $key (keys (%{$varsetup{$section}})) {
  43.             next if ($key =~ /^PROD$/i);    # to avoid infinite loop
  44.             $varsetup{$section}{$key} =~ s/\$PROD/$product/iog;
  45.         }
  46.     }
  47.  
  48.     # Determine filename matching keyword
  49.     $keyword = $varsetup{'COPY'}{'filename'};
  50.     $keyword =~ s/\*/\.*/g;
  51.     print "INFO   : Searching File with Keyword \[$varsetup{'COPY'}{'filename'}\]\n";
  52.  
  53.     # Read the local files - to avoid duplicate copy
  54.     print "INFO   : Reading keyword from LOCAL \[$varsetup{'COPY'}{'filepath'}\] files\n";
  55.     opendir(DIR,"$varsetup{'COPY'}{'filepath'}") or die "Cannot open local directory $varsetup{'COPY'}{'filepath'}\n";
  56.     @localfiles = readdir(DIR);
  57.     closedir(DIR);
  58.  
  59.     foreach $localfile (@localfiles) {
  60.         print "INFO   : Checking $localfile\n";
  61.         if ($localfile =~ /$keyword/io) {
  62.             print "INFO   : Found Local File \[$localfile\] matches $varsetup{'COPY'}{'filename'}\n";
  63.         }
  64.     }
  65.     print "######## COMPLETED FOR $product ########\n";
  66. }
  67.  
==============================

I have created 3 dummy zip files in the path for the debug.
Expand|Select|Wrap|Line Numbers
  1. c:\temp\DATA_GROUP2_20080720.zip
  2. c:\temp\GRAPH_GROUP2_20080720.zip
  3. c:\temp\TABLE_GROUP2_20080720.zip
Your help will be much appreciated.

Thanks.
Jul 31 '08 #1
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
The issue is due to the 'o' option that you have used in your regex:
Expand|Select|Wrap|Line Numbers
  1. if ($localfile =~ /$keyword/io) {
  2.  
Change it to:
Expand|Select|Wrap|Line Numbers
  1. if ($localfile =~ /$keyword/i) {
  2.  
The /o option for regular expression tells Perl to compile the regular expression only once. This is to make it more efficient in case where you are searching for the same pattern again and again.
In your case, you are searching for two different patterns ($keyword) . Using /o will make Perl to look for only the first pattern that was used, though the value of $keyword changes.

- Nithin
Jul 31 '08 #2

P: 13
Hi Nithin,

Thanks for the pointer. You've solved the problem that I've been cracking my head for 2 days. I had the wrong interpretation of option /o. Now it works fine and I'll be able to continue adding the code I need.

Really appreciate your help.
Jul 31 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.