duanliangman5398 2015-03-17 15:25
浏览 67
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • dongtai419309 2015-03-17 15:39
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换