MZH
MZH

Reputation: 1514

PHP Preg_match Regular Expression Results

this is my RE, I'm using it with preg_match_all PHP, but I'm getting not accurate results http://screencast.com/t/FPsoJkAT

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

I'm getting extra rows(empty rows + PM and AM) which are supposed to be skipped

This is the expected result, when I try to test it with RE utility. http://screencast.com/t/RRteFlCD

someone please help me in sorting this RE to match the exact results with the right order as that utility produces.

RE which is not working fine

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

Input

11/01/13 $4.34 10:02PM Internet79kbs $0.02 06:42PM Internet34kbs $0.01 05:37PM0438377989Optus SMS1 $0.000 05:21PM Internet253kbs $0.06 03:59PM0438377989Optus SMS1 $0.000 03:59PM0438377989Optus SMS1 $0.000 03:59PM0438377989Optus SMS1 $0.000 03:57PM0438377989Optus SMS1 $0.000 12:40PM Internet462kbs $0.10 12:34PM Internet1659kbs $0.37 11:26AM Internet54kbs $0.01 11:20AM0411000321DIV-VoiceMail00:00:19 $0.00 11:20AM Internet76kbs $0.02 11:19AM0438377989Optus SMS1 $0.000 11:19AM0438377989Optus SMS1 $0.000 10:02AM0414828271Optus SMS1 $0.000 09:32AM Internet297kbs $0.07 08:46AM0282700517Sydney00:03:15 $3.59 08:40AM0411000321DIV-VoiceMail00:00:25 $0.00 08:39AM Internet388kbs $0.09 Page 1 of 3 Print usage details 16/01/2013 https://www.optus.com.au/portal/site/customercentre /template.BINARYPORTLET/m ... ( Due to processing time, recently made calls or rece nt data usage may not appear in the above usage tra nsaction details page. Some types of included value or discou nts may also not be displayed. If you change your r ate plan or add -on which takes effect mid way through your bill-cy cle, your usage summary will be inaccurate until th e commencement of your next bill cycle. Amounts shown are: GST exclusive for telephony, postpaid mobile and mob ile broadband services. GST inclusive for prepaid mobile services. 10/01/13 $22.94 10:00PM Internet239kbs $0.05 10:00PM Internet46kbs $0.01 08:56PM0417973105Optus SMS1 $0.000 08:56PM0417973105Optus SMS1 $0.000 08:51PM0417973105Optus SMS1 $0.000 08:51PM0417973105Optus SMS1 $0.000 08:45PM0417973105Optus SMS1 $0.000 06:15PM Internet23kbs $0.01 06:00PM Internet156kbs $0.03 06:00PM0738483389Brisbane00:15:18 $13.41 04:22PM0438377989Optus SMS1 $0.000 04:22PM0438377989Optus SMS1 $0.000 02:20PM Apps Usage Mobile152kbs $0.03 02:14PM Internet154kbs $0.03 02:14PM0282700517Sydney00:05:49 $5.23 02:03PM0411000321DIV-VoiceMail00:00:24 $0.00 01:55PM Internet958kbs $0.21 01:54PM0282700517Sydney00:01:04 $1.95 01:53PM0411000321VoiceMail00:00:32 $0.82 12:50PM0411000321DIV-VoiceMail00:00:03 $0.00 12:18PM0295521298Sydney00:00:07 $1.14 10:04AM0411000321DIV-VoiceMail00:00:26 $0.00 08:07AM Internet76kbs $0.02 Page 2 of 3 Print usage details 16/01/2013 https://www.optus.com.au/portal/site/customercentre /template.BINARYPORTLET/m ... Usage balance may not take into account cap inclusi ons/discounts or adjustments. For Postpaid, If you h ave over 7000 records, unbilled usage shown will be approximate. Please refer to your invoice for actual charges. Page 3 of 3 Print usage details 16/01/2013 https://www.optus.com.au/portal/site/customercentre /template.BINARYPORTLET/m ...

Thanks

Upvotes: 0

Views: 124

Answers (1)

Barmar
Barmar

Reputation: 781058

preg_match_all()'s result array is a two-dimensional array. $result[0] is an array of all the matches for the entire RE. The remaining $result[i] are arrays of the ith capture group in each match.

So for your purposes, all you care about is $result[0]. I tested this, and it has 47 elements.

Also, you should probably change all the groups in your RE to non-capturing groups, to avoid generating all these extraneous strings in the first place. So every (...) should be (?:...).

print_r($result[0]) prints:

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
)

Upvotes: 1

Related Questions