I am looking for help parsing a string. Below I have posted the string and what I would ideally like to the values output as.

I was able to parse out the status, but i need to parse out the other results that I am looking for. Any help would be greatly appreciated!

$string = "Fairlight Mustangs 6 Rocanville Reds 1 (FINAL)";

// get status
if( preg_match( '!(([)]+))!', $string, $c ) )
$status = $c[1];

output Fairlight Mustangs 6
output Rocanville Reds 1
output FINAL

    Wouldn't it be easier to just use explode(' ', $str);

    Parts of the string you want to get are just to ambiguous for regular expression imho.

    p.s.
    Hello everyone 🙂

      What are the other results you are looking for? I'm going to take a wild guess that maybe this is something to do with sports results; that you have a team name and a number (of points or something), then another team name and another number, and I guess the thing in parentheses is that "status" you're referring to.

      So I'm going use the terms "team name" and "points value", whatever they may actually be for your purpose.

      Assuming that team names can't contain digits, then the first "team name" would be everything up to but not including the first points value (less things like extra whitespace). Then comes the first points value, which are all the consecutive digits up to the next non-digit. Then there's the second team name which continues up to the next digit, then the second points value, then the (parenthesised) status.

      So, assuming all the assumptions I've had to make are correct, the regular expression would match "non-digits followed by whitespace followed by digits followed by whitespace followed by non-digits followed by whitespace followed by digits followed by whitespace, an open parenthesis, non-closing-parentheses, and a final closing parenthesis".

        One way would be to use this regex:

        <?php
        
        $string = "Fairlight Mustangs 6 Rocanville Reds 1 (FINAL)";
        $regex = '/^(.*?) (\d+) (.*?) (\d+) \(([^\)]+)/';
        if(preg_match($regex, $string, $matches))
        {
            echo <<<EOD
        $matches[5]
        $matches[1]: $matches[2]
        $matches[3]: $matches[4]
        
        EOD;
        }
        else {
            echo "No results found";
        }
        

        Output:

        FINAL
        Fairlight Mustangs: 6
        Rocanville Reds: 1
        
          Write a Reply...