title: Regular Expression for SysLog RFC5424
body: <p>I am writing a SysLog Server where my program receive messages of RFC5424Format.</p>
  
  <p>My program has to parse the message and store the values.</p>
  
  <p>I have got a regular expression which is failing to parse the message.</p>
  
  <p>There is problem in Regular Expression.I am new to Regular expression.</p>
  
  <p>Any help appreciated.</p>
  
  <pre><code>public static void Main()
      {
          string RFC5424Format = @"(\&lt;(?&lt;PRI&gt;\d+)\&gt;(?&lt;VERSION&gt;\d+)?)? \ * (?&lt;TIMESTAMP&gt; ( (?&lt;YEAR&gt;\d+) - (?&lt;MONTH&gt;\d+) - (?&lt;DAY&gt;\d+) ) T+ (?&lt;HOUR&gt;\d+): (?&lt;MINUTE&gt;\d+): (?&lt;SECOND&gt;\d+) (\.(?&lt;MILLISECONDS&gt;\d+))? (?&lt;OFFSET&gt;Z|(\+|\-)\d+:\d+)? ) \ (?&lt;HOSTNAME&gt;[\w!-~]+) \ (?&lt;APPNAME&gt;[\w!-~]+) \ (?&lt;PROCID&gt;[\w!-~]+) \ (?&lt;MSGID&gt;[\w!-~]+) \  (?&lt;SD&gt;-|(\[.*\])) \ ?(?&lt;MESSAGE&gt;.*)?";
  
          Regex rfc5424 = new Regex("^" + RFC5424Format + "$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace);
  
          string input = "&lt; 38 &gt; 1 2018 - 03 - 01T16: 05:51.799465 + 05:30 AAEINBLR07229L Source_UDP - -\n ??? MessageContent_Via_UDP - 5424";
  
          Match m = rfc5424.Match(input);
  
          if (m.Success)
          {
              Console.WriteLine("Regex is fine");
          }
          else
          {
              Console.WriteLine("Problem in Regex");
          }
      }
  </code></pre>