Search code examples
phpregextext-parsing

Parse a string without delimiters into a 2d array


I have a string like that:

 0d(Hi)i(Hello)4d(who)i(where)540d(begin)i(began)

And I want to make an array with that. I try first to add separator, in order to use the php function explode.

 ;0,d(Hi),i(Hello);4,d(who),i(where);540,d(begin),i(began)

It works but the problem is I want to minimize the separator to save disk space. Therefore I want to know by using preg_split(), regular expression, if it's possible to have a huge array like that without using separator:

Array (
    [0] => Array (
        [0] => 0
        [1] => d(hi)
        [2] => i(Hello)
    ) 
    [1] => Array (
        [0] => 4
        [1] => d(who)
        [2] => i(where)
    )
    [2] => Array (
        [0] => 540
        [1] => d(begin)
        [2] => i(began)
    )
)

I try some code & regex, but I saw that the value in the regular expression was not present in the final result (like explode function, in the final array we do not have the delimitor.)

More over, i have some difficulties to build the regex. Here is the one that I made :

 $modif = preg_split("/[0-9]+(d(.+))?(i(.+))?/", $data); 

I must precise that d() and i() can not be present (but at least one).


Solution

  • If you do

    preg_match_all('/(\d+)(d\([^()]*\))?(i\([^()]*\))?/', $subject, $result, PREG_SET_ORDER);
    

    on your original string, then you'll get an array where

    $result[$i][0]
    

    contains the ith match (i. e. $result[0][0] would be 0d(Hi)i(Hello)) and where

    $result[$i][$c]
    

    contains the cth capturing group of the ith match (i. e. $result[0][1] is 0, $result[0][2] is d(Hi) and $result[0][2] is i(Hello)).

    Is that what you wanted?