473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

regexp not working

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
2 1530
nithinpes
410 Expert 256MB
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
ahgan
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

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

Similar topics

5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
0
by: Ed Leafe | last post by:
I recently upgraded to 4.1 alpha (MySQL 4.1.0-alpha-standard-log) on my Linux server, and came across a problem with a query that had been working in 3.23 that no longer worked in 4.1a. I've...
10
by: Andrew DeFaria | last post by:
I was reading my O'Reilly JavaScript The Definitive Guide when I came across RegExp and thought I could tighten up my JavaScript code that checks for a valid email address. Why does the following...
5
by: Syed Ali | last post by:
Hello, I am trying to create a regexp to express non letters and space. I tried using: var myreg = new RegExp (""); However, it is not working. Basically I want to allow only words with...
4
by: McKirahan | last post by:
How would I use a regular expression to remove all trailing Carriage Returns and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance. Also, are they any great references for learning...
6
by: Mark Findlay | last post by:
I am trying to figure out how to set up my reg exp search so that the search will only match on the exact word. Here is the current problem code: Word1 = "RealPlayer.exe" Word2 = "Player.exe"...
0
by: Chris Croughton | last post by:
I'm trying to use the EXSLT regexp package from http://www.exslt.org/regexp/functions/match/index.html (specifically the match function) with the libxml xltproc (which supports EXSLT), but...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.