Miguel Sousa
Miguel Sousa

Reputation: 55

Split csv with regex

Hi I have a CSV file with this structure:

A243503  ;H ;ZU, HS-JIM ;7;F122;VO ;A12 ;A2E;U;be;24.11.15 ;05.12.15 ;11^/
; ; 1 ;   1 ;A26 ;24.11.15 ;11:05;14:45;TL;F ;^/
18.10.15 ;18.10.15 ;BU;
    BLABLABLABLA BLABLABLABLA BLABLABLABLA
A243504;F  ;BAS, R   56  ;FC12VOp ;A12;App 2E; ;ern;24.11.15 ;05.12.15;11 ^/
 ;  ; 1 ;2  ;A26;24.11.15  ;11:05 ;14:45 ;TiXL  ;FC  ;   ^/
18.10.15 ;18.10.15 ;BU       ;
A243483 ;H ;BR, WG  ;64  ;F113 ;GR  ;DIS;DZ;H ;Hp ;19.04.16;28.04.16; 9       ^/
 ;  ; 1  ;   1  ;A306   ;19.04.16    ;09:50    ;13:30    ;TX ;NC;            ^/
18.10.15 ;18.10.15 ;BU  ;
    Fr1%
    il. Rg ud Tr a/b

And I need to divide it at each A999999. So I created the following code to do it:

$re = "/(\sA\d{6})/"; 
$result= preg_split($re , $str,-1,PREG_SET_ORDER);
print_r($result);

However what is printed is:

Array
(
    [0] => A243503  ;H ;ZU, HS-JIM ;7;F122;VO ;A12 ;A2E;U;be;24.11.15 ;05.12.15 ;11^/
    ; ; 1 ;   1 ;A26 ;24.11.15 ;11:05;14:45;TL;F ;^/
    18.10.15 ;18.10.15 ;BU;
        BLABLABLABLA BLABLABLABLA BLABLABLABLA

    [1] =>  A243504
    [2] => ;F  ;BAS, R   56  ;FC12VOp ;A12;App 2E; ;ern;24.11.15 ;05.12.15;11 ^/
     ;  ; 1 ;2  ;A26;24.11.15  ;11:05 ;14:45 ;TiXL  ;FC  ;   ^/
    18.10.15 ;18.10.15 ;BU       ;

    [3] =>  A243483
    [4] =>  ;H ;BR, WG  ;64  ;F113 ;GR  ;DIS;DZ;H ;Hp ;19.04.16;28.04.16; 9       ^/
     ;  ; 1  ;   1  ;A306   ;19.04.16    ;09:50    ;13:30    ;TX ;NC;            ^/
    18.10.15 ;18.10.15 ;BU  ;
        Fr1%
        il. Rg ud Tr a/b
)

But I wanted it to be something like:

Array
(
    [0] => A243503  ;H ;ZU, HS-JIM ;7;F122;VO ;A12 ;A2E;U;be;24.11.15 ;05.12.15 ;11^/
    ; ; 1 ;   1 ;A26 ;24.11.15 ;11:05;14:45;TL;F ;^/
    18.10.15 ;18.10.15 ;BU;
        BLABLABLABLA BLABLABLABLA BLABLABLABLA

    [1] =>  A243504 ;F  ;BAS, R   56  ;FC12VOp ;A12;App 2E; ;ern;24.11.15 ;05.12.15;11 ^/
     ;  ; 1 ;2  ;A26;24.11.15  ;11:05 ;14:45 ;TiXL  ;FC  ;   ^/
    18.10.15 ;18.10.15 ;BU       ;

    [2] =>  A243483 ;H ;BR, WG  ;64  ;F113 ;GR  ;DIS;DZ;H ;Hp ;19.04.16;28.04.16; 9       ^/
     ;  ; 1  ;   1  ;A306   ;19.04.16    ;09:50    ;13:30    ;TX ;NC;            ^/
    18.10.15 ;18.10.15 ;BU  ;
        Fr1%
        il. Rg ud Tr a/b
)

I want the matched text to go together with the rest..

Upvotes: 0

Views: 66

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

With this pattern:

$result = preg_split('~\s*(?=A[0-9]{6})~', $str, -1, PREG_SPLIT_NO_EMTPY);

details:

~             # pattern delimiter
\s*           # trailing whitespaces of the previous occurrence (including newline)
(?=A[0-9]{6}) # lookahead: followed by "A" and 6 digits
~

Upvotes: 1

Ephedra
Ephedra

Reputation: 881

Try it this way. I cant test it myself so I am not sure it works:

$re = "/(\sA\d{6})/"; 
$result= preg_split($re , $str);
print_r($result);

Upvotes: 0

Related Questions