You could always use a parser:
$grammar = <<'__END_OF_GRAMMAR__'; { use strict; use warnings; $skip = qr/[ \t]*/; } # Returns a ref to an array of lines. parse : line(s?) /$/ { $item[1] } # Returns a ref to an array of atoms and lists. line : term(s?) /\n/ { $item[1] } # Returns an atom or a list. # An atom looks like: [ atom => "text" ] # A list is a ref to an array of atoms and/or lists. term : '{' term(s?) '}' { [ list => $item[2] ] } | /[^{} \t\n]+/ { [ atom => $item[1] ] } __END_OF_GRAMMAR__
Update: Added missing closing square brackets.
Update: Tested:
use strict; use warnings; use Data::Dumper (); use Parse::RecDescent (); $::RD_HINT = 1; #$::RD_TRACE = 1; my $grammar = <<'__END_OF_GRAMMAR__'; { use strict; use warnings; $skip = qr/[ \t]*/; } # Returns a ref to an array of lines. parse : line(s?) /$/ { $item[1] } # Returns a ref to an array of atoms and lists. line : term(s?) /\n/ { $item[1] } # Returns an atom or a list. # An atom looks like: [ atom => "text" ] # A list is a ref to an array of atoms and/or lists. term : '{' term(s?) '}' { [ list => $item[2] ] } | /[^{} \t\n]+/ { [ atom => $item[1] ] } __END_OF_GRAMMAR__ my $p = Parse::RecDescent->new($grammar) or die("Bad grammar\n"); my $text = <<'__END_OF_TEXT__'; DataLine1 {error 1 1 {^E 0-20-9:0-50-9:0-50-9.*$} {^E 0-20-9:0-50-9:0- +50-9.*$}} {three 1 1 {^.*35=A.*$|^.*35=5.*$} {^.*35=A.*$|^.*35=5.*$}} + {fixv 1 1 ^.*VFIXFxProxy.*Disconnected ^.*VFIXFxProxy.*Disconnected} DataLine2 error 1 1 {^E 0-20-9:0-50-9:0-50-9.*$} {^E 0-20-9:0-50-9:0-5 +0-9.*$} {three 1 1 {^.*35=A.*$|^.*35=5.*$} {^.*35=A.*$|^.*35=5.*$}} { +fixv 1 1 ^.*VFIXFxProxy.*Disconnected ^.*VFIXFxProxy.*Disconnected} __END_OF_TEXT__ my $t = $p->parse($text) or die("Bad text\n"); print(Data::Dumper::Dumper($t)); __END__ output (reformatted for readability) ====== [ # Array of lines. [ # First line. [ atom => 'DataLine1' + ], [ list => [ [ atom => 'error' + ] , [ atom => 1 + ] , [ atom => 1 + ] , [ list => [ [ atom => '^E' + ] , [ atom => '0-20-9:0-50-9 +:0-50-9.*$' ] ] ] , [ list => [ [ atom => '^E' + ] , [ atom => '0-20-9:0-50-9 +:0-50-9.*$' ] ] ] ], [ list => [ [ atom => 'three' + ] , [ atom => 1 + ] , [ atom => 1 + ] , [ list => [ [ atom => '^.*35=A.*$|^. +*35=5.*$' ] ] ] , [ list => [ [ atom => '^.*35=A.*$|^. +*35=5.*$' ] ] ] ], ... ], [ # Second line. ... ], ]
Note: It's quite easy to modify the grammar to generate data in a different format. Let me know if you need help to do so.
In reply to Re: multiple matches in regex
by ikegami
in thread multiple matches in regex
by ww
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |