Miles_Prower
Miles_Prower

Reputation: 13

How Do I split a string with parentheses and commas using regex?

"Func(1,2),Func(Sub(1,Func(true)),2),Func(Sub(1,false),3)"
[1] = "Func(1,2)"
[2] = "Func(Sub(1,Func(true)),2)"
[3] = "Func(Sub(1,false),3)"

I Have this string. And I have to use pcre-regex to seperate this into X parts. The parts should be splitted at the comma. What gives me the most trouble is the fact that these function-strings could also contain further sub-function-strings. So I guess it's only possible to identify the split-position by counting parantheses.

/.*(?=\().*(?=\)\,)/gU

All my attempts to solve this problem didn't work.

Upvotes: 1

Views: 61

Answers (1)

splash58
splash58

Reputation: 26153

Using recursive regex

(?:\w+(?:\((?:(?:[^()]+|(?R))*)\)),?)+

demo and some explanations

Upvotes: 1

Related Questions