I need a regular expression that will match a comma that is NOT between either a '[' and ']' or '(' and ')' or '{' and '}'. Other grouping symbols do not matter. I have tried to figure it out but I cannot come up with anything that accomplishes this.
The regex is to be used with the PHP preg_split function to split a string on the matched commas.
An example string containing commas and grouping symbols:
<div>Hello<div>,@func[opt1,opt2],{,test},blahblah
The string should split up as follows:
1: '<div>Hello<div>'
2: '@func[opt1,opt2]'
3: '{,test}'
4: 'blahblah'
And I just thought of this, but at this point all grouping symbols are guaranteed to have matching symbols, incase that helps.
Any help would be GREATLY appriciated =)
Actually it is not impossible to get this splitting done. Consider this code:
$str = '<div>Hello<div>,(foo,bar),@func[opt1,opt2],{,test},blahblah';
$arr = preg_split('~([^,]*(?:{[^}]*}|\([^)]*\)|\[[^]]*])[^,]*)+|,~', $str, -1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
var_dump($arr);
array(5) {
[0]=>
string(15) "<div>Hello<div>"
[1]=>
string(9) "(foo,bar)"
[2]=>
string(16) "@func[opt1,opt2]"
[3]=>
string(7) "{,test}"
[4]=>
string(8) "blahblah"
}
I don't think it can be done in a regular expression. The basic problem is that this requires variable length negative look-behinds (disallow any [({ that is not followed by a ])}), and that isn't a capability which RE currently has.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With