1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/usr/bin/perl use strict; use warnings; my $string = '[1,962,[["x",0,"0.1","1.106"]]][1,963,[["x",0,"0.663","0.0"]]]'; $string .= '[1][1,562,[["x",0,"0.579","1.1"]]][1,563,[["x",0,"0.546","0.0"],["x",0,"0.94","1.8"]]]'; my @matches = ( $string =~ m/(\[.+?\])\[/g ); for (@matches) { print "$_\n"; } __DATA__ Erhofft: [1,962,[["x",0,"0.1","1.106"]]] [1,963,[["x",0,"0.663","0.0"]]] [1] [1,562,[["x",0,"0.579","1.1"]]] [1,563,[["x",0,"0.546","0.0"],["x",0,"0.94","1.8"]]] Realitaet: [1,962,[["x",0,"0.1","1.106"]]] [["x",0,"0.663","0.0"]]] [1,562,[["x",0,"0.579","1.1"]]]
1
2
3
4
my @matches = ( $string =~ m/(\[.+\])\[/g );
my @matches = ( $string =~ m/(\[.+\])\[|/g );
my @matches = ( $string =~ m/(\[.+?\])\[|/g );
my @matches = ( $string =~ m/(\[.+?\])[\[|]/g );
my @matches = ( $string =~ m/(\[.+?\])(?=\[|\z)/g );
1
2
my $string = '[1,962,[["x",0,"0.1","1.106"]]][1,963,[["x",0,"0.663","0.0"]]]';
$string .= '[1][1,562,[["x",0,"0.579","1.1"]]][1,563,[["x",0,"0.546","0.0"],["x",0,"0.94","1.8"]]]';
QuotePS: Dein JSON String erzeugt bei mir eine Fehlermeldung.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
my $string = '[1,962,[["x",0,"0.1","1.106"]]][1,963,[["x",0,"0.663","0.0"]]]';
$string .= '[1][1,562,[["x",0,"0.579","1.1"]]][1,563,[["x",0,"0.546","0.0"],["x",0,"0.94","1.8"]]]';
Erhofft:
[1,962,[["x",0,"0.1","1.106"]]]
[1,963,[["x",0,"0.663","0.0"]]]
[1]
[1,562,[["x",0,"0.579","1.1"]]]
[1,563,[["x",0,"0.546","0.0"],["x",0,"0.94","1.8"]]]
Realitaet:
[1,962,[["x",0,"0.1","1.106"]]]
[["x",0,"0.663","0.0"]]]
[1,562,[["x",0,"0.579","1.1"]]]
my @matches = ( $string =~ m/(\[.+?\])(?=\[|\z)/g );
2017-11-12T10:07:24 styx-ccDas ist schon richtig, aber eben Theorie.
In der Praxis kommen die Daten auch mal als Stream und die Sequenzen werden schneller in den Stream geschrieben, als man Sie auslesen kann (z.B. IPC via pipe).
Dann ist man gezwungen die aneinandergereihten Sequenzen (siehe String) wieder in einzelne zu zerlegen, um überhaupt via JSON-Algorithmus vermittelbare Sequenzen zur Verfügung zu haben.
Lieben Gruß und auch Dir einen schönen Sonntag
print CHILD $json;
print CHILD $json . "\n";