how can I match these:
(1, 'asd', 'asd2')
but not match this:
(1, '(data)', 0)
I want to match the ( and ), but not match ( and ) inside ( and ).
Actually these are queries and I want to split them via preg_split.
/[\(*\)]+/
splits them, but also splits ( and ) inside them, how can I fix this?
Example:
The data is:
(1, ‘user1’, 1, 0, 0, 0)(2, ‘user(2)’, 1, 0, 0, 1)
I want to split them as:
Array(
0 => (1, 'user1', 1, 0, 0, 0)
1 => (2, 'user(2)', 1, 0, 0, 1)
);
instead of it, its splitted as:
Array(
0 => (1, 'user1', 1, 0, 0, 0)
1 => (2, 'user
2 => 2
3 => ', 1, 0, 0, 1)
);
A regex for this would be a little nasty. Instead, you can iterate over the entire string and decide where to split:
), split there. (I’m assuming the brackets are balanced in the string and can’t be nested)', ignore any)until a closing'(If it can be escaped, you can look at the previous characters for an odd number of\).I think this is a more straight-forward solution than a regex.