Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Multiline regexp question

by andreas1234567 (Vicar)
on Dec 16, 2013 at 13:56 UTC ( #1067332=perlquestion: print w/replies, xml ) Need Help??

andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

Monks

I use AFPPARSER.EXE" /D from www.btbnet.de to convert AFP files to text. The following script to outputs
C:\test\perl>perl tle.pl ENVELOPEID 36 CUSTOMER_NAME 36 KUNDNAVN 36 ZIP_CODE 36 COUNTRY 36 FAKTURA 36 KONTRAKT 36 REGISTER 36 DATO 36
I would like the following script to output the text between the quotes following FQName together with the last quoted text between to 'TLE' elements:
ENVELOPEID 5172986 CUSTOMER_NAME Acme Inc KUNDNAVN BANANA AIRLINES ZIP_CODE SE-971 74 ..
The script:
use strict; use warnings; use 5.010; my $file; { local $/; $file = <DATA>; } while ($file =~ /TLE.*?FQName\W+'(.*?)'.*?'(.*?)'.*?(TLE)?/gs ) { say "$1 $2"; } __DATA__ .11893 ... TLE - Tag Logical Element 00033 D3 +A090 00000 | |Fully Qualified Name Triplet 2 (x'02') |- Type 11 (x'0B') Attribute GID |- Format 0 (x'00') Character string |- FQName 'ENVELOPEID' |Attribute Value Triplet 54 (x'36') |- '5172986' .11894 ... TLE - Tag Logical Element 00059 D3 +A090 00000 | |Fully Qualified Name Triplet 2 (x'02') |- Type 11 (x'0B') Attribute GID |- Format 0 (x'00') Character string |- FQName 'CUSTOMER_NAME' |Attribute Value Triplet 54 (x'36') |- 'Acme Inc' .11895 ... TLE - Tag Logical Element 00074 D3 +A090 00000 | |Fully Qualified Name Triplet 2 (x'02') |- Type 11 (x'0B') Attribute GID |- Format 0 (x'00') Character string |- FQName 'KUNDNAVN' |Attribute Value Triplet 54 (x'36') |- 'BANANA AIRLINES' .11896 ... TLE - Tag Logical Element 00033 D3 +A090 00000 | |Fully Qualified Name Triplet 2 (x'02') |- Type 11 (x'0B') Attribute GID |- Format 0 (x'00') Character string |- FQName 'ZIP_CODE' |Attribute Value Triplet 54 (x'36') |- 'SE-971 74' .11897 ... TLE - Tag Logical Element 00025 D3 +A090 00000 | |Fully Qualified Name Triplet 2 (x'02') |- Type 11 (x'0B') Attribute GID |- Format 0 (x'00') Character string |- FQName 'COUNTRY' |Attribute Value Triplet 54 (x'36') |- 'SE' .11898 ... TLE - Tag Logical Element 00030 D3 +A090 00000 | |Fully Qualified Name Triplet 2 (x'02') |- Type 11 (x'0B') Attribute GID |- Format 0 (x'00') Character string |- FQName 'FAKTURA' |Attribute Value Triplet 54 (x'36') |- '5643452' .11899 ... TLE - Tag Logical Element 00032 D3 +A090 00000 | |Fully Qualified Name Triplet 2 (x'02') |- Type 11 (x'0B') Attribute GID |- Format 0 (x'00') Character string |- FQName 'KONTRAKT' |Attribute Value Triplet 54 (x'36') |- '90234342' .11900 ... TLE - Tag Logical Element 00031 D3 +A090 00000 | |Fully Qualified Name Triplet 2 (x'02') |- Type 11 (x'0B') Attribute GID |- Format 0 (x'00') Character string |- FQName 'REGISTER' |Attribute Value Triplet 54 (x'36') |- 'AF89729' .11901 ... TLE - Tag Logical Element 00028 D3 +A090 00000 | |Fully Qualified Name Triplet 2 (x'02') |- Type 11 (x'0B') Attribute GID |- Format 0 (x'00') Character string |- FQName 'DATO' |Attribute Value Triplet 54 (x'36') |- '16.12.13'
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Replies are listed 'Best First'.
Re: Multiline regexp question
by hdb (Monsignor) on Dec 16, 2013 at 14:08 UTC

    Change your regex to

    /FQName\W+'(.*?)'.*?\|- '(.*?)'/gs
Re: Multiline regexp question
by Athanasius (Bishop) on Dec 17, 2013 at 02:32 UTC

    A variation: If the data file is large, you may want to avoid slurping it all into memory. In which case, set the input record separator $/ to a suitable string:

    #! perl use strict; use warnings; use feature 'say'; { local $/ = 'TLE - Tag Logical Element'; while (<DATA>) { say "$1 $2" if /FQName\s+'(.*?)'.*\|-\s+'(.*?)'/s; } } __DATA__ <as before>

    (Regex based on hdb’s ++answer.) Output:

    12:17 >perl 802_SoPW.pl ENVELOPEID 5172986 CUSTOMER_NAME Acme Inc KUNDNAVN BANANA AIRLINES ZIP_CODE SE-971 74 COUNTRY SE FAKTURA 5643452 KONTRAKT 90234342 REGISTER AF89729 DATO 16.12.13 12:17 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1067332]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2019-05-23 01:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (140 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!