使用正则表达式分割字符串,以逗号(,)为分隔符,但如果逗号在花括号{ }中,则忽略该逗号。

4
我需要一个正则表达式来使用逗号(,)分割字符串,但在以下示例中忽略花括号{,}中的逗号;
"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"

我尝试了这个方法,但它也会将大括号{ }内的逗号分开。

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

但是我完全不知道这应该以什么样的方式结束。任何帮助都将不胜感激!

2个回答

4
您可以使用以下正则表达式进行拆分。
(,)(?=(?:[^}]|{[^{]*})*$)

因此,使用preg_split,您可以像这样完成它:

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

正则表达式


1
我看到两种可能性(不会因为长字符串而崩溃)

第一种使用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 (study) 修饰符来改进模式:

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

或者您可以像这样不进行修改地编写:

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

这种方式会使用比正则表达式引擎的普通遍历更快的算法来搜索带有{,的位置。


我还想问一件事,因为你似乎在正则表达式方面很擅长,如果你不介意回答的话……我该如何排除?names={"index"="user.all", "show"="user.view"} FROM "asd",domain={"id"="test"},names={"index"="user.all", "show"="user.view"},test="test" = {} 大括号表单应该被忽略,其余部分应由逗号分隔符解析。 - Oguzhan
@Oguzhan:你所询问的超出了正则表达式的范围,你需要使用SQL解析器。 - Casimir et Hippolyte

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接