using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?P<timestamp>[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}-[0-9]{4}) (?P<severity>[A-Z]) (?P<component>[A-Z]+) ?\[ ?(?P<context>[a-zA-Z0-9 ]+)] (?<message>[^\n]+)";
string input = @"MongoDB
2014-11-03T18:28:32.450-0500 I NETWORK [initandlisten] waiting for connections on port 27017
2019-04-09T14:03:15.546-0400 I COMMAND [conn1] command test.items appName: ""MongoDB Shell"" command: aggregate { aggregate: ""items"", pipeline: [ { $match: { a: { $gte: 4.0 } } } ], cursor: {}, lsid: { id: UUID(""042b423f-91bb-43bd-a378-4657ad4e0b35"") }, $db: ""test"" } planSummary: COLLSCAN cursorid:7185821004643502676 keysExamined:0 docsExamined:25000 hasSortStage:1 numYields:289 nreturned:101 reslen:1305014 locks:{ Global: { acquireCount: { r: 387 } }, Database: { acquireCount: { r: 387 } }, Collection: { acquireCount: { r: 387 } } } storage:{ data: { bytesRead: 6808584, timeReadingMicros: 4336 } } protocol:op_msg 34439ms
2014-11-03T18:28:32.450-0500
";
RegexOptions options = RegexOptions.Multiline;
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