0

Here is the code I am testing that does not match my pattern:

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string test = "Error during request to host: 123.456.78, port: 1234 with status 500 and message={\n    \"messageID\": \"TEST_ID\",\n    \"messageArgs\": [\n        \"12345\"\n    ],\n    \"message\": \"The test having ID \\\"12345\\\" was not found.\",\n    \"recommendedActions\": []\n}";
        Match m = Regex.Match(test, @"message=({.*})", RegexOptions.IgnoreCase | RegexOptions.Multiline);
        Console.WriteLine(m.Success);
        Console.WriteLine(test);
        if(m.Success){
            Console.WriteLine(m.Groups[0]);
            Console.WriteLine(m.Groups[1]);
        }
    }
}

For some reason this does not work, but I believe the pattern is correct to capture what is inside of message={}.

Here is it working in regex 101: https://regex101.com/r/yY2bQ4/1

user1634494
  • 609
  • 1
  • 7
  • 23
  • 1
    Use `Regex.Match(test, @"message=({.*})", RegexOptions.IgnoreCase | RegexOptions.Singleline)`. And use [RegexStorm.net](http://regexstorm.net/tester) to test .NET patterns as regex101 does not support .NET regex syntax. – Wiktor Stribiżew Jul 28 '16 at 18:03
  • The "." character doesn't match newline. Also, the string you entered into regex 101 is NOT the same as the string that C# actually renders. If you do a Console.writeline you get something more akin to: message={ "messageID" : "TEST_ID", "messageArgs": [ "12345" ], "message" : "The test having ID \"12345\" was not found.", "recommendedActions": [] } – EJoshuaS - Stand with Ukraine Jul 28 '16 at 18:22
  • @WiktorStribiżew - may I post my comment as an answer? – EJoshuaS - Stand with Ukraine Jul 28 '16 at 18:23

0 Answers0