PHP Preg_match正则表达式结果

时间:2013-02-07 00:23:58

标签: php regex

这是我的RE,我正在使用preg_match_all PHP,但我的结果并不准确 http://screencast.com/t/FPsoJkAT

#(\d+/\d+/\d+)|(\d+:\d+(AM|PM).*?[$]\d)#i

我正在获得额外的行(空行+ PM和AM),这些行应该被跳过

当我尝试使用RE实用程序测试它时,这是预期的结果。 http://screencast.com/t/RRteFlCD

有人请帮助我对此RE进行排序,以便将该确切结果与该实用程序生成的正确顺序相匹配。

RE不能正常工作

 preg_match_all("#(\d+/\d+/\d+)|(\d+:\d+(AM|PM).*?[$]\d)#i",$response,$result);

输入

11/01/13 $ 4.34 10:02 PM Internet34kbs $ 0.02 06:42 PM Internet34kbs $ 0.01 05:37 PM0438377989Optus SMS1 $ 0.000 05:21 PM Internet253kbs $ 0.06 03:59 PM0438377989Optus SMS1 $ 0.000 03:59 PM0438377989Optus SMS1 $ 0.000 03:59 PM0438377989Optus SMS1 $ 0.000 03:57 PM0438377989Optus SMS1 $ 0.000 12 :40PM Internet462kbs $ 0.10 12:34 PM Internet1659kbs $ 0.37 11:26 AM Internet54kbs $ 0.01 11:20 AM0411000321DIV-VoiceMail00:00:19 $ 0.00 11:20 AM Internet76kbs $ 0.02 11:19 AM0438377989Optus SMS1 $ 0.000 11:19 AM0438377989Optus SMS1 $ 0.000 10:02 AM0414828271Optus SMS1 $ 0.000 09:32 AM Internet297kbs $ 0.07 08 :46AM0282700517Sydney00:03:15 $ 3.59 08:40 AM0411000321DIV-VoiceMail00:00:25 $ 0.00 08:39 AM Internet388kbs $ 0.09第1页,共3页打印使用细节16/01/2013 https://www.optus.com.au/portal/site/customercentre /template.BINARYPORTLET/m ...(到期对于处理时间,最近拨打电话或接收数据的使用可能不会出现在上述使用情况详细信息页面中。某些类型的包含值或不公开内容也可能无法显示。如果您更改了计划或广告d -on在您的账单中途生效,您的使用汇总将在下一个账单周期开始前不准确。显示的金额为:GST专用于电话,后付费移动和移动宽带服务。 GST包含预付费移动服务。 10/01/13 $ 22.94 10:00 PM Internet239kbs $ 0.05 10:00 PM Internet46kbs $ 0.01 08:56 PM0417973105Optus SMS1 $ 0.000 08:56 PM0417973105Optus SMS1 $ 0.000 08:51 PM0417973105Optus SMS1 $ 0.000 08:51 PM0417973105Optus SMS1 $ 0.000 08:45 PM0417973105Optus SMS1 $ 0.000 06:15 PM Internet23kbs $ 0.01 06:00 PM Internet156kbs $ 0.03 06:00 PM0738483389Brisbane00:15:18 $ 13.41 04:22 PM0438377989Optus SMS1 $ 0.000 04:22 PM0438377989Optus SMS1 $ 0.000 02:20 PM Apps Usage Mobile152kbs $ 0.03 02:14 PM Internet154kbs $ 0.03 02:14 PM0282700517Sydney00:05:49 $ 5.23 02:03 PM0411000321DIV-VoiceMail00:00:24 $ 0.00 01 :55PM Internet958kbs $ 0.21 01:54 PM0282700517Sydney00:01:04 $ 1.95 01:53 PM0411000321VoiceMail00:00:32 $ 0.82 12:50 PM0411000321DIV-VoiceMail00:00:03 $ 0.00 12:18 PM0295521298Sydney00:00:07 $ 1.14 10:04 AM0411000321DIV-VoiceMail00:00:26 $ 0.00 08: 07AM Internet76kbs $ 0.02第2页,共3页打印使用详情16/01/2013 https://www.optus.com.au/portal/site/customercentre /template.BINARYPORTLET/m ...使用余额可能无法考虑上限/折扣或调整。对于后付费,如果您拥有超过7000条记录,则显示的未开票用量将为近似值。请参阅您的发票了解实际费用。第3页,共3页打印使用详情16/01/2013 https://www.optus.com.au/portal/site/customercentre /template.BINARYPORTLET/m ...

由于

1 个答案:

答案 0 :(得分:1)

preg_match_all()的结果数组是一个二维数组。 $result[0]是整个RE的所有匹配的数组。剩余的$result[i]是每个匹配中i捕获组的数组。

因此,出于您的目的,您关心的只是$result[0]。我对此进行了测试,它有47个元素。

此外,您应该将RE中的所有组更改为非捕获组,以避免首先生成所有这些无关的字符串。因此,每个(...)应为(?:...)

print_r($result[0])打印:

Array
(
    [0] => 11/01/13
    [1] => 10:02PM Internet79kbs $0
    [2] => 06:42PM Internet34kbs $0
    [3] => 05:37PM0438377989Optus SMS1 $0
    [4] => 05:21PM Internet253kbs $0
    [5] => 03:59PM0438377989Optus SMS1 $0
    [6] => 03:59PM0438377989Optus SMS1 $0
    [7] => 03:59PM0438377989Optus SMS1 $0
    [8] => 03:57PM0438377989Optus SMS1 $0
    [9] => 12:40PM Internet462kbs $0
    [10] => 12:34PM Internet1659kbs $0
    [11] => 11:26AM Internet54kbs $0
    [12] => 11:20AM0411000321DIV-VoiceMail00:00:19 $0
    [13] => 11:20AM Internet76kbs $0
    [14] => 11:19AM0438377989Optus SMS1 $0
    [15] => 11:19AM0438377989Optus SMS1 $0
    [16] => 10:02AM0414828271Optus SMS1 $0
    [17] => 09:32AM Internet297kbs $0
    [18] => 08:46AM0282700517Sydney00:03:15 $3
    [19] => 08:40AM0411000321DIV-VoiceMail00:00:25 $0
    [20] => 08:39AM Internet388kbs $0
    [21] => 16/01/2013
    [22] => 10/01/13
    [23] => 10:00PM Internet239kbs $0
    [24] => 10:00PM Internet46kbs $0
    [25] => 08:56PM0417973105Optus SMS1 $0
    [26] => 08:56PM0417973105Optus SMS1 $0
    [27] => 08:51PM0417973105Optus SMS1 $0
    [28] => 08:51PM0417973105Optus SMS1 $0
    [29] => 08:45PM0417973105Optus SMS1 $0
    [30] => 06:15PM Internet23kbs $0
    [31] => 06:00PM Internet156kbs $0
    [32] => 06:00PM0738483389Brisbane00:15:18 $1
    [33] => 04:22PM0438377989Optus SMS1 $0
    [34] => 04:22PM0438377989Optus SMS1 $0
    [35] => 02:20PM Apps Usage Mobile152kbs $0
    [36] => 02:14PM Internet154kbs $0
    [37] => 02:14PM0282700517Sydney00:05:49 $5
    [38] => 02:03PM0411000321DIV-VoiceMail00:00:24 $0
    [39] => 01:55PM Internet958kbs $0
    [40] => 01:54PM0282700517Sydney00:01:04 $1
    [41] => 01:53PM0411000321VoiceMail00:00:32 $0
    [42] => 12:50PM0411000321DIV-VoiceMail00:00:03 $0
    [43] => 12:18PM0295521298Sydney00:00:07 $1
    [44] => 10:04AM0411000321DIV-VoiceMail00:00:26 $0
    [45] => 08:07AM Internet76kbs $0
    [46] => 16/01/2013
    [47] => 16/01/2013
)