using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^\s*Failed\s(.*)([\s\S]*?)(Stack\sTrace:\s*at\s)(.*)\s+in\s+(.*):line\s+(\d+)\s*$";
string input = @"Test run for /home/runner/work/Library/Library.Tests/bin/Debug/net7.0/Library.Tests.dll (.NETCoreApp,Version=v7.0)
Microsoft (R) Test Execution Command Line Tool Version 17.7.0-preview-23364-03+bc17bb9693cfc4778ded51aa0ab7f1065433f989 (x64)
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.55] Library.Tests.UnitTest1.DeliberateFail2 [FAIL]
[xUnit.net 00:00:00.56] Library.Tests.UnitTest1.DeliberateFail1 [FAIL]
Failed Library.Tests.UnitTest1.DeliberateFail2 [< 1 ms]
Error Message:
System.Exception : Error message
Stack Trace:
at Library.Tests.UnitTest1.DeliberateFail2() in /home/runner/work/Library/Library.Tests/UnitTest1.cs:line 30
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Failed Library.Tests.UnitTest1.DeliberateFail1 [< 1 ms]
Error Message:
Assert.True() Failure
Expected: True
Actual: False
Stack Trace:
at Library.Tests.UnitTest1.DeliberateFail1() in /home/runner/work/Library/Library.Tests/UnitTest1.cs:line 27
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Results File: /home/runner/work/Library/Library.Tests/TestResults/test-results.trx
Failed! - Failed: 2, Passed: 133, Skipped: 0, Total: 135, Duration: 549 ms - Library.Tests.dll (net7.0)
Error: Process completed with exit code 1.";
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx