简体   繁体   English

用正则表达式解析字符串

[英]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 matching this in another way? 我认为,这是因为单词中的[^)] *或[^]] *,但是我应该如何以其他方式更正regexp以使其匹配?

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 却什么也没有

好吧,我用这个,它有效

preg_match_all('/(?:\s{1}([+-]{1})\s{1}){0,}'.word.'/', $right, $matches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM