parse string with regular expression
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 because of [^)]* or [^]]* in the word, but how I should correct regexp for match开发者_运维技巧ing 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
well, i use this, and it works
preg_match_all('/(?:\s{1}([+-]{1})\s{1}){0,}'.word.'/', $right, $matches);
精彩评论