I found several posts describing the format of messages compliant with msbuilds parsing of errors and warnings but I could not find a regular expression that would do the same thing.
msbuild visual studio aware error messages and message formats
So I worked one out that I think works in most cases
const RegexOptions OPTIONS = RegexOptions.Compiled |
RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace |
RegexOptions.ExplicitCapture |
RegexOptions.Singleline;
// in multi-line $ == LF but not CR so use [^\r\n]+
const string PATTERN = @"
# token to match microsoft standard error format for tools
(^(
((?
(?:(?:[a-z]:|\\\\[a-z0-9_.$-]+\\[a-z0-9_.$-]+)\\| # Drive or UNC
\\?[^\\/:*?<>|\r\n]+\\?) # Relative path
(?:[^\\/:*?<>|\r\n]+\\)* # Folder
[^\\/:*?<>|\r\n]* # File name
(\((?
|(?
(\w+\s)? # optional severity
(?
(\s\w+\d)? # error code - optional
:\s* # white space
(?
)
";
It looks really scary but it is reasonably simple if you take it a chunk at a time.
Named captures: