2012-12-27 95 views
1

我有形式有錯誤的文件:PHP正則表達式和preg_match_all()問題

================================================ 
    Header of file with date and time 
    ================================================ 
    Loaded options from XML file: 'path/to/file/some_file.xml 
    extendedPrintPDF started 
    extendedPrintPDF: Error: Unsaved documents have no full name.; line: 332 
    ================================================ 
    Header of file with date and time 
    ================================================ 
    Error opening document: path/to/file/some_file1: Error: Either the file does not exist, you do not have permission, or the file may be in use by another application; line: 190 
    Error opening document: path/to/file/some_file2: Error: Either the file does not exist, you do not have permission, or the file may be in use by another application; line: 190 

我使用

preg_match_all('/Error: (.*)/m', $file_data, $erroenames,PREG_PATTERN_ORDER); 

獲得陣列中的所有錯誤。 它似乎在第一組中的錯誤正常工作。但是,以'Error Opening document'開頭的第二組錯誤似乎顯示爲數組的單個元素,因此,我只有4個元素而不是9個數。但是,當我在http://www.spaweditor.com/scripts/regex/index.php上嘗試相同的操作時,所有的錯誤顯示爲數組的不同元素,我得到9個元素。 有人能告訴我我做錯了什麼嗎? 我試過使用|併爲「錯誤打開文檔」創建一個RegEx。但是,即使這似乎不起作用。

+0

如何檢索'$ file_data'?我嘗試了成功的正則表達式...... –

+1

也許「錯誤打開文檔」條目不能用換行符分隔(例如
)。 – periklis

+0

@ Tiger-222:我正在使用file_get_contents。這是 的延續http://stackoverflow.com/questions/13995685/reading-errors-and-file-names-from-a-log-txt-file –

回答

0

我懷疑第二組數據中行尾的\ r和/或\ n字符,因爲是的,這應該起作用。嘗試在十六進制編輯器中查看輸出文件。

另一個想法是使用錨點和非貪婪*。

'/Error: (.*?)$/m' 
+0

好的!因此,即使它的分隔符問題和我複製粘貼相同的東西在我的原始問題中共享的鏈接的RegEx測試,測試人員照顧它? 並且不, '/錯誤:(。*?)$/m' 也不起作用。 :( –

+0

@WatchfulProtector我不會指望測試者「接收」文件中實際的行尾字符,複製和粘貼可能會爲您清理它們。使用十六進制編輯器查看實際文件是要走的路。 – EvilBob22

0

你可以嘗試以下方法:

/error[^:]*: ([^:\n]+(?:line:\s*\d+)?)/i 

這將需要各種錯誤,並在下一站:或行結束,然後回溯會踢,給你如果行號有是任何