doufei9805 2015-04-21 00:22
浏览 27
已采纳

试图学习为PHP编写RegEx

I am trying to learn how to write a RegEx but it seems all my searches lead to unclear information. So my question is two fold.

  1. Does anyone have a good source on how a newbie could learn to write RegExs?
  2. How could I write a RegEx that breaks the string 1y 311d 16h 42m into variables?

I'm looking to take the above text string and break it into something like:

$duration[years] = 1;
$duration[days] = 311;
$duration[hours] = 16;
$duration[minutes] = 42;

Please note the total digits might may not always be the same for example it could be two digit days. Something like. 25d or some could be omitted. I might just get days and hours. Lastly the order might change. Perhaps it is written days then years etc.

I know I could do this easily with an explode function and strpos, but I really want to learn Regex so I am using this as an example as I understand they can be very powerful for things like this.

  • 写回答

1条回答 默认 最新

  • doukekui0914 2015-04-21 00:39
    关注

    1) Some useful pages:

    2) Specifically, this:

    $pattern = '/(?:(?P<years>\d+)y\s*)?(?:(?P<days>\d+)d\s*)?(?:(?P<hours>\d+)h\s*)?(?:(?P<minutes>\d+)m\s*)?/';
    preg_match($pattern, '1y 311d 16h 42m', $duration);
    print_r($duration);
    // Array
    // (
    //     [0] => 1y 311d 16h 42m
    //     [years] => 1
    //     [1] => 1
    //     [days] => 311
    //     [2] => 311
    //     [hours] => 16
    //     [3] => 16
    //     [minutes] => 42
    //     [4] => 42
    // )
    preg_match($pattern, '311d 42m', $duration);
    print_r($duration);
    // Array
    // (
    //     [0] => 1y 311d 16h 42m
    //     [years] =>
    //     [1] =>
    //     [days] => 311
    //     [2] => 311
    //     [hours] =>
    //     [3] =>
    //     [minutes] => 42
    //     [4] => 42
    // )
    

    This will give fixed order though. If the order can change, regexp is not a good tool. It's still possible in this case, but rather awkward. Here it is:

    $pattern = "/(?=.*?(?:(?P<years>\d+)y|$))(?=.*?(?:(?P<days>\d+)d|$))(?=.*?(?:(?P<hours>\d+)h|$))(?=.*?(?:(?P<minutes>\d+)m|$))/";    
    preg_match($pattern, '311d 16h 1y', $duration);
    print_r($duration);
    // Array
    // (
    //     [0] =>
    //     [years] => 1
    //     [1] => 1
    //     [days] => 311
    //     [2] => 311
    //     [hours] => 16
    //     [3] => 16
    // )
    

    Entering these patterns (without the leading and trailing slashes) in regex101 will give you the explanation of what exactly it is trying to match. Find other examples from the regex tag questions and enter them as well, and try to see how they work. Experience is the best teacher.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题
  • ¥15 一道计算机组成原理问题