dongsu4345 2019-08-02 06:05
浏览 233
已采纳

根据空格和括号从字符串中提取单词

I have some strings that correspond to words separated by spaces and that can be grouped using parenthesis. I also have an array of strings that represent the groups present in the string (not necessarily in the same order).

Here is an example :

myString = "zzz AND (aaa AND (bbb AND ccc OR ddd) ) OR eee AND \"fff  fff\" AND (ggg)"
myGroups = ["(ggg)", "(aaa AND (bbb AND ccc OR ddd) )"]

I want to split that query on spaces that are not inside quotes and that are not inside parenthesis. The output of the split on the previous string would be as follow :

["zzz", "AND", "(aaa AND (bbb AND ccc OR ddd) )", "OR", "eee", "AND", "\"fff  fff\"", "AND", "(ggg)"]

I succeeded to split on spaces that are not inside quotes using the regex

\s(?=(?:[^'"`]*(['"`])[^'"`]*\1)*[^'"`]*$)

But I am struggling with adding the notion of groups in this.

Note :

  • myString may contain nested groups, but I only want to split on the outer one
  • No need to handle cases where parenthesis syntax is wrong
  • 写回答

2条回答 默认 最新

  • douwu5009 2019-08-02 07:21
    关注
    // PHP 
    $re = '/(\((([^()]*|(?1))*)\))|(\\\\"[^\\\\]+\\\\"|[^\s]+)/m';
    $str = 'zzz AND (aaa AND (bbb AND ccc OR ddd) ) OR eee AND \\"fff  fff\\" AND (ggg)';
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    $result = array_column($matches, 0);
    print_r($result);
    
    // output
    Array
    (
        [0] => zzz
        [1] => AND
        [2] => (aaa AND (bbb AND ccc OR ddd) )
        [3] => OR
        [4] => eee
        [5] => AND
        [6] => \"fff  fff\"
        [7] => AND
        [8] => (ggg)
    )
    
    

    Regex Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码