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.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP