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  => a  => [b,c,d]  => e(f)  => g(h + i) = j  => a + c = g(e)
Thanks in advance.php regex share edited Jun 14 '14 at 5:36 asked Jun 12 '14 at 9:25 PhatHV 4,702 4 21 37
closed as unclear what you're asking by OGHaza, Robin, 웃웃웃웃웃, hjpotter92, Sergey Telshevsky Jun 13 '14 at 9:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.
7 Given you have almost 3k rep, you should know better than to post a "gimme teh codez" question... – OGHaza Jun 12 '14 at 9:27 1 This seems like the wrong approach, why can't you change your data? all you need is a space after that first comma. – fin Jun 12 '14 at 9:31 The space in my string is not important. So, it can be every where not only after the first comma. – PhatHV Jun 12 '14 at 9:37 1 Are nested strucures like a,[b,[c,d],e] a possibility? – Taemyr Jun 12 '14 at 10:05 1 How about empty entries like a,,b,c,d? Or other edge cases? - Notwithstanding the current answers, for forming suitable expressions one needs to know what assumptions can be made about the input. – Taemyr Jun 12 '14 at 10:19 | show 4 more comments
2 Answers 2active oldest votes up vote 2 down vote accepted
preg_match_all to get the matches
.+or you'll match too much in
[a,b],c,[d]. Though we know nothing of the possible input... – Robin Jun 12 '14 at 10:01 Good point, edited the answer. – user887675 Jun 12 '14 at 10:15 I trimmed all space and used below RE for expanded cases like (a,b,c),g(a,b,c),g(a,b)=f(c,d),g(a,b)=f(c,d)+h(e,f),g(a,b)=f(c,d)+h(e,f),g(a,b)-f(c,d)=h(e,f),g(a,b)*f(c,d)+h(e,f)=f(t,y). the RE is /(([|()[^])]+(]|))|[^,]?\w*([^)]+()=(?R)|)-(?R)|);+(?R)|)*(?R)|)\/(?R)|)|)|[^,]+)/g – PhatHV Jun 13 '14 at 7:29 (([|()[^])]+(]|))|([^,]?\w*(|[^,]?[\w.]*=[\w.]*()[^)]+()=(?R)|)-(?R)|)+(?R)|)*(?R)|)\/(?R)|)|)|[^,]+) – PhatHV Jun 13 '14 at 8:04 add a comment | up vote 1 down vote
The following regexp returns a match for each of the entries in your array.
share edited Jun 12 '14 at 10:03 answered Jun 12 '14 at 9:58 Taemyr 1,805 6 15 Your answer also solved my problems,too. – PhatHV Jun 12 '14 at 10:22 add a comment |