tags:

views:

118

answers:

2

I'm trying to parse a fragment of a stacktrace, which looks pretty much like this:

at Test.Test1() in C:\Projects\Project\Test.cs:line 37

Using a regex like this works as intended:

at (.*?) in (.*?):line (\d*)

This matches

  1. Test.Test1()
  2. C:\Projects\Project\Test.cs
  3. 37

This regex is hardcoded to an english stacktrace, so there are obviously no matches if the stacktrace is in another language, such as swedish:

vid Test.Test1() i C:\Projects\Project\Test.cs:rad 37

To make the matching more language neutral I tried this regex out:

 (.*?) .*? (.*?) (\d*)

This matches

  1. Test.Test1()
  2. C:\Projects\Project\Test.cs:line
  3. 37

The question is how would I match the file path without the trailing :line?

+1  A: 

(.?) .? (.?):(\S+) (\d)

I am presuming that the spaces between the matches are actually matching the spaces in the regex. The colon will likely be constant in all languages, so you just need to match non-whitespace characters after the colon

Edit:

Had a play around and came up with this:

.+?\s+(\S+)\s+.+?\s+(.*):(\S*)\s(\d+)

for

at Test.Test1() in C:\Projects\Project Folder\Test.cs:rad 37

The colon in the path was throwing me for a second. but this should have

Test.Test1() in $1 C:\Projects\Projects Folder\Test.cs in $2 rad in $3 37 in $4

Xetius
Yes your assumption is correct. Apart from readability are there any other differences between our regexes? I presume your version is more efficent?
Blake Pettersson
+2  A: 

You could try hard-coding the necessity of having a colon at the second place in the file name part:

.:[^:]*

After the colon following the drive letter there obviously can't be any further colon that would be part of the file name. You might have to cope with UNC paths, though, so the following might fix that:

.:?[^:]*

which makes the colon optional to allow for UNC paths.

So, your captures for the file name part with the ":line" following might look like this:

(.:?[^:]*):\S+
Joey
Just what I was looking for, cheers!
Blake Pettersson
If you know that the last colon in the whole line separates the path from the line word (or whatever it is) you could try using a greedy form of searching for : so (.*):(\S+) will match that. This will then handle relative paths without colon or even old Apple Mac paths using colon as a path separator
Xetius