Link to home
Start Free TrialLog in
Avatar of troesler
troesler

asked on

Perl multiline regex only returns first match

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.
SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the question and the points.
Avatar of troesler
troesler

ASKER

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