除了大括号内的另一个大括号外,我想用括号内的逗号分隔
$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);
最终结果应为:
for $ q1:
array(
0 => id INTEGER
1 => code DECIMAL (4,2)
2 => PRIMARY KEY (id)
);
for $ q2:
array(
0 => id INTEGER
1 => code TEXT
);
答案 0 :(得分:1)
代码:(PHP Demo)
IntentService
输出:
$('form[name="paypal"]').submit(function () {
if (invalid) {
alert("Some of the fields are not valid.");
return false;
}
return true;
});
正则表达式细分:(Regex 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'
)
允许排除捕获组,(?:^.+?\(|,) #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
数组,其大小是具有捕获组的数组的一半。
答案 1 :(得分:0)
如果embeded括号仅包含数字,则执行以下操作:
$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] =>
)