[Last Call] Learn how to a build a cloud-first strategyRegister Now


Perl multiline regex only returns first match

Posted on 2007-08-02
Medium Priority
Last Modified: 2012-05-05
I'm trying to do a multiline regex on a file and print out ALL matching entries, however only the first one is being returned.
I'm sure I'm missing something obvious/fundamental.

Here is a sample of the file contents, showing 2 entries that should be returned:

    RECORD   : T011                                                                                                                  
    TRANSACTION TYPE : 010 REFERRENCE NUMBER : 14110207300000000823302  
    TRANSACTION AMT : 00000010000M    /OP0            
          NO.01 ITEM           : RECORD TYPE                                                                                        
                CODE & MESSAGE : 0301 TRAILER RECORD DOES NOT EXIST.                                                                
                CONTENTS       :                                                                                                    
    LICENSEE ID(HEADER) : 141102                                                    CENTRAL PROC DATE  : 07/31/2007                  
    PROCESSING DATE     : 07/30/2007       SEQUENCE NUMBER : 002                    VERIFICATION TIME  : 07/31/2007                  
    CURRENCY  : P00                                                                    (START -  END)  : 05:02:45 - 05:02:52        
                                              TOTAL COUNT   :      1           TOTAL AMOUNT   :                0.000                
                                                  PROCESSED COUNT :      0           PROCESSED AMOUNT :                0.000        
                                                  ERRONEOUS COUNT :      1           ERRONEOUS AMOUNT :                0.000        
    RECORD   : 0110                                                                                                                  
    TRANSACTION TYPE : 107 REFERRENCE NUMBER : 41102073000000008233221  
    TRANSACTION AMT : 0000010000MO    /P00            
          NO.01 ITEM           : RECORD TYPE                                                                                        
                CODE & MESSAGE : 0301 TRAILER RECORD DOES NOT EXIST.                                                                
                CONTENTS       :                                                                                                    

Here is the relevant code snip as it is currently:

      local $/ = undef;
      open (FH, "<", $v_filename_full)
        or die "Could not open file ($v_filename_full): $!";
      while (<FH>)
        print "$1,R,$2,$3" if (/.*REFER+ENCE\sNUMBER\s:\s([\d]{23}).*CODE\s&\sMESSAGE\s:\s(\d+)\s(.*\.)/sg);

The output I'm after would be:
14110207300000000823302,R,0301,TRAILER RECORD DOES NOT EXIST.                                                                
41102073000000008233221,R,0301,TRAILER RECORD DOES NOT EXIST.                                                                

But only the first line is being returned:
14110207300000000823302,R,0301,TRAILER RECORD DOES NOT EXIST.                                                                

Thanks in advance.
Question by:troesler
  • 2
LVL 27

Assisted Solution

ddrudik earned 150 total points
ID: 19620075
This modification seemed to work for me:
       print "$1,R,$2,$3" if (/.*?REFER+ENCE\sNUMBER\s:\s([\d]{23}).*?CODE\s&\sMESSAGE\s:\s(\d+)\s(.*?\.)/sg);
LVL 39

Accepted Solution

Adam314 earned 600 total points
ID: 19620131
The while loop should not loop on <FH> because with $/ set to undef, you will read the entire file at once.  Also, the regex has a few bugs in it.  Try like this:

local $/ = undef;
open (FH, "<", $v_filename_full) or die "Could not open file ($v_filename_full): $!";
print "$1,R,$2,$3\n" while (/REFER+ENCE\sNUMBER\s:\s([\d]{23}).*?CODE\s&\sMESSAGE\s:\s(\d+)\s(.*?\.)/sg)
LVL 27

Expert Comment

ID: 19620206
Thanks for the question and the points.

Author Comment

ID: 19620359
Thanks Guys - I used Adam314's solution and it is working as expected.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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.…
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
Suggested Courses

831 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