使用正则表达式对数据进行分组,它是孩子们的

I have a simple document that I need to split up into events (by day), unfortunately the document contains other useless info (such as event details) which I'll need to crawl through to retrieve the info. An except of this document looks like this:

10th March 2015
Baseball 10:00 Please remember to bring your bats
Soccer 14:00 over 18s only

11th March 2015
Swimming 10:00 Children only
Soccer 14:00 Over 14s team training

My initial plan was to use preg_spit to try and split the string at the date, then loop over each one, however I need to maintain the structure of the document.

Ideally I'd like to return the data into an array like:

arr[
   'days' =>[
        'date' => '10th MArch 2015'
        'events' => ['Baseball 10:00', 'Soccer 14:00'],
    ]
]

How would I best go about doing this? Regex isn't my strongest suit, but I know enough to capture the days ([0-9]{1,2}[a-z]{2}/s[a-z]+/s[0-9]{4}) and the events ([a-Z]+/s[0-9]{2}:[0-9]{2}).

You can use this regex:

/(?:\b(\d+th\h+.*?\d{4})\b|\G)\s+(\S+\h+\d{2}:\d{2}\b).*?(?=\s+(?>\S+\h+\d{2}:\d{2}|\d+th\h+|\z))/i

And then a bit of PHP code to loop through the result.

RegEx Demo

This is what I came up with. I used explode() to split out the different sections and then to split up the lines. I didn't use preg_match() until the very end to get the specific sport/time.

<?php
$text = <<<EOD
10th March 2015
Baseball 10:00 Please remember to bring your bats
Soccer 14:00 over 18s only

11th March 2015
Swimming 10:00 Children only
Soccer 14:00 Over 14s team training
EOD;

$days = array();

if( $sections = explode("

",$text) ){
    foreach($sections as $k=>$section){

        $events = array();

        $lines = explode("
",$section);

        $day = $lines[0];

        unset($lines[0]);

        if($lines){
            foreach($lines as $line){
                preg_match("/(\w+)\s(\d){2}:(\d){2}/",$line,$matches);
                if(isset($matches[0])){
                    $events[] = $matches[0];
                }

            }
        }

        $days[$k] = array(
            'day' => $day,
            'events' => $events
        );

    }

}

echo '<pre>',print_r($days),'</pre>';