如何在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
use preg_match_all
to get the matches
答
The following regexp returns a match for each of the entries in your array.
/(?<=^|,)(?:([^\[\],]*)|(\[([^\[\]]*)\]))(?=$|,)/g