如何在PHP中用逗号分隔解析数学表达式的正则表达式[关闭]

如何在PHP中用逗号分隔解析数学表达式的正则表达式[关闭]

问题描述:

I have this string a,[b,c,d], e(f), g(h + i) = j, a + c = g(e)

How can I write a regex to parse it to array like below?

=> Array
     [1] => a
     [2] => [b,c,d]
     [3] => e(f)
     [4] => g(h + i) = j
     [5] => a + c = g(e)

Thanks in advance.

/(\[[^]]+\]|[^,]+)/g

http://regex101.com/r/xV2uA6

use preg_match_all to get the matches

http://www.php.net/preg_match_all

The following regexp returns a match for each of the entries in your array.

/(?<=^|,)(?:([^\[\],]*)|(\[([^\[\]]*)\]))(?=$|,)/g