1

I have a file that has errors in the form:

    ================================================
    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

I am using

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

to get all the errors in an array. It seems to work fine for the errors in the first set. But, the errors in second set that start with 'Error Opening document' seem to display as a SINGLE element of the array and so, I have just 4 elements of the array instead of 9. However, when I try the same thing on http://www.spaweditor.com/scripts/regex/index.php, all errors display as different elements of the array and I get 9 elements. Can some one tell me what I am doing wrong? I tried using | and creating one more RegEx for "Error opening document". But, even that doesnt seem to work.

2 Answers2

0

I suspect the \r and/or \n characters at the end of the lines in the second set of data because, yeah, that should work. Try looking at the output file in a hex editor.

Another idea would be to use an anchor and a non-greedy *.

'/Error: (.*?)$/m'
EvilBob22
  • 732
  • 5
  • 12
  • Okay!! So, even if its a delimiter problem and I copy paste the same stuff on the RegEx tester whose link I shared in my original question, the tester takes care of it? And no, '/Error: (.*?)$/m' doesnt work either. :( – Watchful Protector Dec 27 '12 at 22:47
  • @WatchfulProtector I wouldn't count on the tester to "receive" the actual end of line characters in the file, the copy and paste could potentially clean them up for you. Looking at the actual file with a hex editor is the way to go. – EvilBob22 Dec 27 '12 at 23:05
0

You could try the following:

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

this will take all sorts of errors and stop at the next : or line end, then backtracking will kick in and give you the line number if there is any

Anubis
  • 481
  • 4
  • 4