0

これは私の RE です。PHP で使用していpreg_match_allますが、正確な結果が得られません 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: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 ページ 1/3 使用状況の詳細を印刷 16/01/2013 https://www.optus.com.au/portal/site/customercentre/template.BINARYPORTLET/m...請求サイクルの途中で有効になるレート プランまたはアドオンを変更すると、次の請求サイクルが開始されるまで、使用状況の概要が不正確になります。 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 ページ 2/3 使用状況の詳細を印刷 16/01/201321 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ページ 2/3 使用状況の詳細を印刷 16/01/201321 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ページ 2/3 使用状況の詳細を印刷 16/01/2013https://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 ...

ありがとう

4

1 に答える 1

1

preg_match_all()の結果配列は 2 次元配列です。$result[0]RE全体のすべての一致の配列です。残りは、各マッチ$result[i]の th キャプチャ グループの配列です。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
)
于 2013-02-07T00:45:58.870 に答える