parse string with regular exression

Posted by llamerr on Stack Overflow See other posts from Stack Overflow or by llamerr
Published on 2010-06-03T11:42:41Z Indexed on 2010/06/03 11:44 UTC
Read the original article Hit count: 228

Filed under:
|

I trying to parse this string:

$right = '34601)S(1,6)[2] - 34601)(11)[2] + 34601)(3)[2,4]';

with following regexp:

const word = '(\d{3}\d{2}\)S{0,1}\([^\)]*\)S{0,1}\[[^\]]*\])';
preg_match('/'.word.'{1}(?:\s{1}([+-]{1})\s{1}'.word.'){0,}/', $right, $matches);
print_r($matches);

i want to return array like this:

Array
(
    [0] => 34601)S(1,6)[2] - 34601)(11)[2] + 34601)(3)[2,4]
    [1] => 34601)S(1,6)[2]
    [2] => -
    [3] => 34601)(11)[2]
    [4] => +
    [5] => 34601)(3)[2,4]
)

but i return only following:

Array
(
    [0] => 34601)S(1,6)[2] - 34601)(11)[2] + 34601)(3)[2,4]
    [1] => 34601)S(1,6)[2]
    [2] => +
    [3] => 34601)(3)[2,4]
)

i think, its becouse of [^)]* or [^]]* in the word, but how i should correct regexp for matching this in another way?

i tryied to specify it:

\d+(?:[,#]\d+){0,}

so word become

const word = '(\d{3}\d{2}\)S{0,1}\(\d+(?:[,#]\d+){0,}\)S{0,1}\[\d+(?:[,#]\d+){0,}\])';

but it gives nothing

© Stack Overflow or respective owner

Related posts about php

Related posts about regex