如何使用单个参数获取多个正则表达式匹配项?

时间:2019-07-05 20:03:16

标签: php regex

我想使用RegEx提取文本“ Received:by”之后的时间,但是它有很多值,并且我想在一个参数中存储很多值。

我已经这样创建了一个RegEx:

Received:by.+(?P<Hop_Time>\d\d:\d\d:\d\d)

但是我只得到了Hop_time的最后一个值。

这是我要从中提取Hop_time的文本类型的示例:

Received:by mail-ed1-f69.google.com with SMTP id b21so4825939edt.18          for <budsarin.temsawat@aaa.co.th>; Wed  26 Jun 2019 18:04:32 -0700 (PDT) X-Google-DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed;          d=1e100.net; s=20161025;          h=x-gm-message-state:mime-version:references:in-reply-to:from:date           :message-id:subject:to:cc;          bh=q04dTGosad+dTkX0DLE3TZT1LvNQX45YXfBh9AMeFco=;          b=a5lzAB+U25QwdRDIw/0H1CT0SGRZNRyiTRNJ1jg5kgfg4IPhyOeWkqm72Ay           w1VV8x3kVzv2jgAyF9/W57NcN43CyejW+ag086fJy2LmqMM03q/Voz7A89av           nxsiIx0V5BOWkcSl3XdBOqQD9imxNL9Bermp548jum51kOopBX6uALrW+Yy           qfSravlh8ONQ8YTOC47iSPBQhCh0b1lMO51p5e6zeIwcLUO62YYl/N3DUKDeN           vduYOj/HAEVQXqfO64Fepx6U2QAi2Lg6hM2zcdKMgDR/Wdj/QehmFj+Fl           Ptag== X-Gm-Message-State:APjAAAUcvfjYPCkT4REkr2t4IE6NeIZTGRWloRcKwy0gyZHb   MynBUQciUYH9gQifGU0TaLv4ddVqHoFhsAXIsniMfUuXeetryhOwO+1bS26ze2SA   fvZIq6qEaOgJlMJZt9nUOMYE2WspxuBWVosL2kKutE99i5q9VzsMDuK+2wlVL4y   MZOpqMIpA6mfKRnzX1cwrcvA9PPbiWI/x7Ldg0kx+kCRuuJQ== X-Received:by 2002:a17:906:3ed1:: with SMTP id d17mr602603ejj.285.1561597403255;          Wed  26 Jun 2019 18:03:23 -0700 (PDT) X-Google-Smtp-Source:APXvYqw4u4lwcGK+asj4XNave7dmVhuneOG1zr46FOmbsgkj0gup9KvdYrzB1LJXS2zMNXf+I= X-Received:by 2002:a17:906:3ed1:: with SMTP id d17mr602584ejj.285.1561597403013;   Wed  26 Jun 2019 18:03:23 -0700 (PDT) MIME-Version:1.0 References:<CAB-UEWaNHyJz4EyotLZA@mail.gmail.com>   <CAB-UEWavdVz0J+ySP5u_GGZm+DVRg@mail.gmail.com>   <CANAFqSiHpjuU5VNQ57MSwc9FuGfwMEu49EvSQ@mail.gmail.com>   <CAB-UEWZBhEzT2QxwsBk-XBfzf7j0qHr5O7tyUxwQw@mail.gmail.com> <CANAFqSiKEMRGkFsA2awnVg5zKSmih6iGOAv7Ng@mail.gmail.com> In-Reply-To:<CANAFqSiKEMRGkFsA2=4wnVg5zKSmih6iGOAv7Ng@mail.gmail.com> From:Sirisri Jitvatkun <sirisri.jitvatkun@bbb.co.th> Date:Thu  27 Jun 2019 08:03:11 +0700

在此示例文本中,我想存储Hop_time的三个值:"18:04:32""18:03:23""18:03:23"

我可以使用RegEx这样做吗?如果是这样,我应该如何修改现有尝试以使其起作用?

2 个答案:

答案 0 :(得分:1)

使用非贪婪的量词,以使.+不会跨越模式的多次出现。

Received:by.+?(?P<Hop_Time>\d\d:\d\d:\d\d)

DEMO

How can I write a regex which matches non greedy?

答案 1 :(得分:0)

此表达式也可能起作用:

Received\s*:\s*by.*?(?P<Hop_Time>[0-9]{2}:[0-9]{2}:[0-9]{2})

(在this demo右上角进行了说明,如果您想进一步探索或修改它,在this link中,您可以逐步观察它如何与某些示例输入匹配) ,如果您愿意。

测试

$re = '/Received\s*:\s*by.*?(?P<Hop_Time>[0-9]{2}:[0-9]{2}:[0-9]{2})/s';
$str = 'Received:by mail-ed1-f69.google.com with SMTP id b21so4825939edt.18          for <budsarin.temsawat@aaa.co.th>; Wed  26 Jun 2019 18:04:32 -0700 (PDT) X-Google-DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed;          d=1e100.net; s=20161025;          h=x-gm-message-state:mime-version:references:in-reply-to:from:date           :message-id:subject:to:cc;          bh=q04dTGosad+dTkX0DLE3TZT1LvNQX45YXfBh9AMeFco=;          b=a5lzAB+U25QwdRDIw/0H1CT0SGRZNRyiTRNJ1jg5kgfg4IPhyOeWkqm72Ay           w1VV8x3kVzv2jgAyF9/W57NcN43CyejW+ag086fJy2LmqMM03q/Voz7A89av           nxsiIx0V5BOWkcSl3XdBOqQD9imxNL9Bermp548jum51kOopBX6uALrW+Yy           qfSravlh8ONQ8YTOC47iSPBQhCh0b1lMO51p5e6zeIwcLUO62YYl/N3DUKDeN           vduYOj/HAEVQXqfO64Fepx6U2QAi2Lg6hM2zcdKMgDR/Wdj/QehmFj+Fl           Ptag== X-Gm-Message-State:APjAAAUcvfjYPCkT4REkr2t4IE6NeIZTGRWloRcKwy0gyZHb   MynBUQciUYH9gQifGU0TaLv4ddVqHoFhsAXIsniMfUuXeetryhOwO+1bS26ze2SA   fvZIq6qEaOgJlMJZt9nUOMYE2WspxuBWVosL2kKutE99i5q9VzsMDuK+2wlVL4y   MZOpqMIpA6mfKRnzX1cwrcvA9PPbiWI/x7Ldg0kx+kCRuuJQ== X-Received:by 2002:a17:906:3ed1:: with SMTP id d17mr602603ejj.285.1561597403255;          Wed  26 Jun 2019 18:03:23 -0700 (PDT) X-Google-Smtp-Source:APXvYqw4u4lwcGK+asj4XNave7dmVhuneOG1zr46FOmbsgkj0gup9KvdYrzB1LJXS2zMNXf+I= X-Received:by 2002:a17:906:3ed1:: with SMTP id d17mr602584ejj.285.1561597403013;   Wed  26 Jun 2019 18:03:23 -0700 (PDT) MIME-Version:1.0 References:<CAB-UEWaNHyJz4EyotLZA@mail.gmail.com>   <CAB-UEWavdVz0J+ySP5u_GGZm+DVRg@mail.gmail.com>   <CANAFqSiHpjuU5VNQ57MSwc9FuGfwMEu49EvSQ@mail.gmail.com>   <CAB-UEWZBhEzT2QxwsBk-XBfzf7j0qHr5O7tyUxwQw@mail.gmail.com> <CANAFqSiKEMRGkFsA2awnVg5zKSmih6iGOAv7Ng@mail.gmail.com> In-Reply-To:<CANAFqSiKEMRGkFsA2=4wnVg5zKSmih6iGOAv7Ng@mail.gmail.com> From:Sirisri Jitvatkun <sirisri.jitvatkun@bbb.co.th> Date:Thu  27 Jun 2019 08:03:11 +0700
';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

var_dump($matches);