regexp使用逗号(,)分隔符拆分字符串,但如果逗号位于大括号{,}中,则忽略


regexp to split a string using comma(,) delimiter but ignore if the comma is in curly braces{,}

我需要一个regexp来使用逗号(,)分隔符分割字符串,但如果逗号在下面的示例中是大括号{,},则忽略它;

"asd", domain={"id"="test"}, names={"index"="user.all", "show"="user.view"}, test="test"

INTO(应该是)

"asd"
domain={"id"="test"}
names={"index"="user.all", "show"="user.view"}
test="test"

问题:(不是这个)

"asd"
domain={"id"="test"}
names={"index"="user.all"
"show"="user.view"}
test="test"

尝试过,但它也在大括号{,}内拆分逗号

'{[^}]*}|[^,]+

但我完全不知道这应该如何结束。任何帮助都会被通知!

您可以使用以下正则表达式来拆分

(,)(?=(?:[^}]|{[^{]*})*$)

所以使用preg_split,你可以像一样完成它

echo preg_split('/(,)(?=(?:[^}]|{[^{]*})*$)/',$your_string);

Regex

我注意到了(不会用长字符串崩溃)的可能性:

第一个与preg_match_all:

$pattern = '~
(?:
    'G(?!'A), # contigous to the previous match, not at the start of the string
  |           # OR
    'A ,??    # at the start of the string or after the first match when
              # it is empty
)'K           # discard characters on the left from match result
[^{,]*+       # all that is not a { or a ,
(?:
    {[^}]*}? [^{,]* # a string enclosed between curly brackets until a , or a {
                    # or an unclosed opening curly bracket until the end
)*+
~sx';
if (preg_match_all($pattern, $str, $m))
    print_r($m[0]);

第二个是preg_split和回溯控制动词,以避免包含在大括号之间的部分(较短,但对于长字符串效率较低)

$pattern = '~{[^}]*}?(*SKIP)(*F)|,~';
print_r(preg_split($pattern, $str));

(*F)强制模式失败,(*SKIP)强制正则表达式引擎在模式失败时跳过已经匹配的部分。

最后一种方法的弱点是模式从交替开始。这意味着,对于不是{,的每个字符,交替的两个分支都会被测试(免费)。但是,您可以使用S(研究)修饰符来改进图案

$pattern = '~{[^}]*}?(*SKIP)(*F)|,~S';

或者你可以不加修改地写,比如:

$pattern = '~[{,](?:(?<={)[^}]*}?(*SKIP)(*F))?~';

通过这种方式,之前使用比正则表达式引擎的正常遍历更快的算法搜索具有{,的位置。