Regex interrupt string based on and / or in search concept

Hi, I am trying to create a regex expression, and I ran into problems.

Basically this is what I have:

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

What I'm looking for:

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

And my expected result that I would like to have is:

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

Basically, OR will break down based on "()", which is optional, so I can have something like this

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

Which I expect to be like this:

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

If I need to have more than one regular expression, I will find with this, The ultimate goal is something like this for the above:

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"
        )
)

If it's something like this

(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))

Then I would expect something like this

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
                )
        )
)

Any help would be greatly appreciated!

+2
source share
2 answers

, , !!!

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

        )

)

- php - -

+3

, explode() PHP, Regex.
( ), .

<?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);

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
                )

        )

)
+2

All Articles