Regex break string based on and/or in a search concept

后端 未结 2 1629
甜味超标
甜味超标 2020-12-21 16:29

Hi I am trying to create regex expression and I am running into problems.

Basically this is what I have:

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

What I

相关标签:
2条回答
  • 2020-12-21 17:07

    Try this, but I used explode() method in PHP and not Regex.
    The input should have every user on a line, and without parenthesis (you can modify the code to remove the parenthesis), but at least this is the concept.

    <?php
    
    $string = 'user email ends with "@domain.com" and user name is "Bob" or user id is 5
    user email ends with "@domain.com" and user name is "Bob" or user id is 5';
    
    echo"<pre>";
    
    //final array
    $result = array();
    
    //separate each line as element in an array
    $line = explode("\n",$string);
    
    //iterate through each line
    for($k=0; $k<count($line);$k++){
    
        //find the and first and separate 
        $and = explode("and",$line[$k]);
        $and_size = count($and);
    
        //find the or in each separted and
        for($i=0;$i<$and_size;$i++){
            $or = explode("or",$and[$i]);
            //place found ors in a new subarray
            if(count($or) > 1){
                $and[] = array();
                $lastId = count($and)-1;
                for($j=1; $j<count($or);$j++){
                    $and[$lastId][] = $or[$j];
                }
            }
        }
        $result[] = $and;
    }
    
    
    
    print_r($result);
    

    Output:

    Array
    (
        [0] => Array
            (
                [0] => user email ends with "@domain.com" 
                [1] =>  user name is "Bob" or user id is 5
                [2] => Array
                    (
                        [0] =>  user id is 5
                    )
    
            )
    
        [1] => Array
            (
                [0] => user email ends with "@domain.com" 
                [1] =>  user name is "Bob" or user id is 5
                [2] => Array
                    (
                        [0] =>  user id is 5
                    )
    
            )
    
    )
    
    0 讨论(0)
  • 2020-12-21 17:08

    Here's a recursive function which also uses a recursive regex, let's recurse!!!

    $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);
        }
    }
    

    Output:

    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

    0 讨论(0)
提交回复
热议问题