title: Json regex deserialization with JSON.Net
body: <p>I'm trying to read the following example json from a text file into a string using the JSON.Net parsing library.</p>
  
  <p>Content of C:\temp\regeLib.json</p>
  
  <pre><code>{
      "Regular Expressions Library":
      {
        "SampleRegex":"^(?&lt;FIELD1&gt;\d+)_(?&lt;FIELD2&gt;\d+)_(?&lt;FIELD3&gt;[\w\&amp;-]+)_(?&lt;FIELD4&gt;\w+).txt$"
      }
  }
  </code></pre>
  
  <p>Example code to try and parse:</p>
  
  <pre><code>Newtonsoft.Json.Converters.RegexConverter rConv = new Newtonsoft.Json.Converters.RegexConverter();
          using (StreamReader reader = File.OpenText(libPath))
          {
            string foo = reader.ReadToEnd();
            JObject jo = JObject.Parse(foo);//&lt;--ERROR
  
            //How to use RegexConverter to parse??
            Newtonsoft.Json.JsonTextReader jtr = new Newtonsoft.Json.JsonTextReader(reader);
            JObject test = rConv.ReadJson(jtr);//&lt;--Not sure what parameters to provide
            string sampleRegex = test.ToString();
          }
  </code></pre>
  
  <p>It seems I need to use the converter, I know the code above is wrong, but I can't find any examples that describe how / if this can be done.  Is it possible to read a regular expression token from a text file to a string using JSON.Net?  Any help is appreciated.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Played with it more and figured out  I had to escape the character classes, once I made the correction below I was able to parse to a JObject and use LINQ to query for the regex pattern.</p>
  
  <p>Corrected content C:\temp\regeLib.json</p>
  
  <pre><code>{
      "Regular Expressions Library":
      {
        "SampleRegex":"^(?&lt;FIELD1&gt;\\d+)_(?&lt;FIELD2&gt;\\d+)_(?&lt;FIELD3&gt;[\\w\\&amp;-]+)_(?&lt;FIELD4&gt;\\w+).txt$"
      }
  }
  </code></pre>
  
  <p>Corrected code</p>
  
  <pre><code>using (StreamReader reader = File.OpenText(libPath))
          {
            string content = reader.ReadToEnd().Trim();
            JObject regexLib = JObject.Parse(content);
            string sampleRegex = regexLib["Regular Expressions Library"]["SampleRegex"].ToString();
  
            //Which then lets me do the following...
            Regex rSampleRegex = new Regex(sampleRegex);
  
            foreach (string sampleFilePath in Directory.GetFiles(dirSampleFiles, "*"))
            {
              filename = Path.GetFileName(sampleFilePath);
              if (rSampleRegex.IsMatch(filename))
              {
                //Do stuff...
              }
            }
          }
  </code></pre>
  
  <p>Not sure if this is the best approach, but it seems to work for my case.</p>