除了大括号内的另一个大括号外,在大括号内用逗号分隔
split by comma inside braces except another braces inside braces
除了大括号内的另一个大括号外,我想在大括号内用逗号分隔
$q1 ="CREATE TABLE notes(id INTEGER,code DECIMAL (4,2),PRIMARY KEY (id))";
$q2 ="CREATE TABLE notes(id INTEGER,code TEXT)";
$r = preg_split('/\([^()]*\)(*SKIP)(*F)|[()]|,/', $q1);//$q1 splitted but $q2 no
print_r($r);
最终结果应该是:
$q1 :
array(
0 => id INTEGER
1 => code DECIMAL (4,2)
2 => PRIMARY KEY (id)
);
$q2 :
array(
0 => id INTEGER
1 => code TEXT
);
如果嵌入的括号仅包含数字,则这样做:
$sqls = array(
"CREATE TABLE notes(id INTEGER,code DECIMAL (4,2),PRIMARY KEY (id))",
"CREATE TABLE notes(id INTEGER,code TEXT)"
);
foreach ($sqls as $sql) {
$arr = preg_split("/(?<!\d)[(),](?!\d)/", $sql);
print_r($arr);
}
输出:
Array
(
[0] => CREATE TABLE notes
[1] => id INTEGER
[2] => code DECIMAL (4,2)
[3] => PRIMARY KEY
[4] => id
[5] =>
[6] =>
)
Array
(
[0] => CREATE TABLE notes
[1] => id INTEGER
[2] => code TEXT
[3] =>
)
代码:(PHP Demo)
$sqls = array(
"CREATE TABLE notes(id INTEGER,code DECIMAL (4,2),PRIMARY KEY (id))",
"CREATE TABLE notes(id INTEGER,code TEXT)"
);
foreach($sqls as $sql){
if(preg_match_all("/(?:^.+?\(|,)(?:\K[\w ]+(?:\([\S].*?\))?)/", $sql,$matches)){
echo "<pre>";
var_export($matches[0]);
echo "</pre>";
}
}
输出:
// first $matches...
array(
0 => 'id INTEGER',
1 => 'code DECIMAL (4,2)',
2 => 'PRIMARY KEY (id)'
)
// second $matches...
array(
0 => 'id INTEGER',
1 => 'code TEXT'
)
正则表达式分解:(Regex Demo)
(?:^.+?\(|,) #group everything from the start to 1st parenthesis or a comma
(?:\K[\w ]+ #\K means "only retain text from this point", group words and spaces
(?:\([\S].*?\))? #optionally group parenthetical text
)
使用 \K
允许排除捕获组和 preg_match_all
returns 第一个子数组中的所需字符串(完整字符串)。好处是 $matches
数组的大小是带有捕获组的数组的一半。
除了大括号内的另一个大括号外,我想在大括号内用逗号分隔
$q1 ="CREATE TABLE notes(id INTEGER,code DECIMAL (4,2),PRIMARY KEY (id))";
$q2 ="CREATE TABLE notes(id INTEGER,code TEXT)";
$r = preg_split('/\([^()]*\)(*SKIP)(*F)|[()]|,/', $q1);//$q1 splitted but $q2 no
print_r($r);
最终结果应该是:
$q1 :
array(
0 => id INTEGER
1 => code DECIMAL (4,2)
2 => PRIMARY KEY (id)
);
$q2 :
array(
0 => id INTEGER
1 => code TEXT
);
如果嵌入的括号仅包含数字,则这样做:
$sqls = array(
"CREATE TABLE notes(id INTEGER,code DECIMAL (4,2),PRIMARY KEY (id))",
"CREATE TABLE notes(id INTEGER,code TEXT)"
);
foreach ($sqls as $sql) {
$arr = preg_split("/(?<!\d)[(),](?!\d)/", $sql);
print_r($arr);
}
输出:
Array
(
[0] => CREATE TABLE notes
[1] => id INTEGER
[2] => code DECIMAL (4,2)
[3] => PRIMARY KEY
[4] => id
[5] =>
[6] =>
)
Array
(
[0] => CREATE TABLE notes
[1] => id INTEGER
[2] => code TEXT
[3] =>
)
代码:(PHP Demo)
$sqls = array(
"CREATE TABLE notes(id INTEGER,code DECIMAL (4,2),PRIMARY KEY (id))",
"CREATE TABLE notes(id INTEGER,code TEXT)"
);
foreach($sqls as $sql){
if(preg_match_all("/(?:^.+?\(|,)(?:\K[\w ]+(?:\([\S].*?\))?)/", $sql,$matches)){
echo "<pre>";
var_export($matches[0]);
echo "</pre>";
}
}
输出:
// first $matches...
array(
0 => 'id INTEGER',
1 => 'code DECIMAL (4,2)',
2 => 'PRIMARY KEY (id)'
)
// second $matches...
array(
0 => 'id INTEGER',
1 => 'code TEXT'
)
正则表达式分解:(Regex Demo)
(?:^.+?\(|,) #group everything from the start to 1st parenthesis or a comma
(?:\K[\w ]+ #\K means "only retain text from this point", group words and spaces
(?:\([\S].*?\))? #optionally group parenthetical text
)
使用 \K
允许排除捕获组和 preg_match_all
returns 第一个子数组中的所需字符串(完整字符串)。好处是 $matches
数组的大小是带有捕获组的数组的一半。