2012-12-27 98 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 

这将需要各种错误,并在下一站:或行结束,然后回溯会踢,给你如果行号有是任何