php - 正则表达式基于和/或基于搜索概念的字符串

原文 标签 php regex

嗨,我试图创建正则表达式,我遇到了问题。

基本上这就是我所拥有的:

(.+?)(and|or)(.+?)


我在找什么例如:

(user email ends with "@email.com" and user name is "John") or (user email ends with "@domain.com" and user name is "Bob")


我希望获得的预期结果是:

(user email ends with "@email.com" and user name is "John")
(user email ends with "@domain.com" and user name is "Bob")


基本上,OR会根据“()”进行拆分,这是可选的,因此我可以拥有类似的内容

user email ends with "@email.com" and user name is "John"


我期望是这样的:

user email ends with "@email.com"
user name is "John"


如果我必须拥有一个以上的正则表达式,那么上面的最终目标是这样的:

Array
(
    [0] => Array
        (
            [0] => user email ends with "@email.com"
            [1] => user name is "John"
        )

    [1] => Array
        (
            [0] => user email ends with "@domain.com"
            [1] => user name is "Bob"
        )
)


如果是这样的话

(user email ends with "@email.com" and user name is "John") or ((user email ends with "@domain.com" and user name is "Bob") or (user id is 5))


那我期待这样的事情

Array
(
    [0] => Array
        (
            [0] => user email ends with "@email.com"
            [1] => user name is "John"
        )

    [1] => Array
        (
            [0] => user email ends with "@domain.com"
            [1] => user name is "Bob"
            [2] => Array
                (
                    [0] => user id is 5
                )
        )
)


任何帮助都感激不尽!

最佳答案

这是一个递归函数,它也使用递归正则表达式,让我们递归!!!

$text = '(user email ends with "@email.com" and user name is "John") or ((user email ends with "@domain.com" and user name is "Bob") or (user id is 5))';

print_r(buildtree($text));

function buildtree($input){
    $regex = <<<'regex'
    ~(                      # Group 1
        \(                  # Opening bracket
            (               # Group 2
                (?:         # Non-capturing group
                    [^()]   # Match anything except opening or closing parentheses
                |           # Or
                    (?1)    # Repeat Group 1
                )*          # Repeat the non-capturing group zero or more times
            )               # End of group 2
        \)                  # Closing bracket
    )~x
regex;
// The x modifier is for this nice and fancy spacing/formatting
    $output = [];

    if(preg_match_all($regex, $input, $m)){ // If there is a match
        foreach($m[2] as $expression){  // We loop through it
            $output[] = buildtree($expression); // And build the tree, recursive !!!
        }
        return $output;
    }else{  // If there is no match, we just split
        return preg_split('~\s*(?:or|and)\s*~i', $input);
    }
}


输出:

Array
(
    [0] => Array
        (
            [0] => user email ends with "@email.com"
            [1] => user name is "John"
        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => user email ends with "@domain.com"
                    [1] => user name is "Bob"
                )

            [1] => Array
                (
                    [0] => user id is 5
                )

        )

)


Online php demo
Online regex demo

相关文章:

php - 生成两个在PHP中不相等的数字

javascript - 用表情符号替换字符串的最有效方法

c# - 用空字符串替换json字符串中的双引号

php - 奇怪的php正则表达式通知

python - 用于正则表达式的Python正则表达式挂起

php - 无法让PHP在IIS Web服务器上运行

php - 添加新的数据库条目时,如何触发刷新(重新加载).php页面?

php - PDO多行插入问题

javascript - 如何用正则表达式进行多次替换,将替换项存储在变量中?

php - zf3 zend导航助手