0

I have the following Regular Expression-

 string s = "{\"data\": {\"words\": [{\"wordsText\": \"Three Elephants /d 
 in the jungle\"}]}}";

    string[] words = s.Split('\\',':','[',']','{','}','"');
    foreach (string word in words)
    {
        Console.WriteLine(word);
    }

Which outputs-

data

words

wordsText

Three Elephants /d in the jungle

What is the best way to get rid of the first 3 lines in the output so that I only get the last line- Three Elephants /d in the jungle.

I believe if I were to write out all text after "wordsText\": this could be a possible method, any help is much appreciated.

2 Answers 2

6

You could use RegEx sure, but since that looks like JSON you would be better off using JSON.NET to parse that.

JObject o = JObject.Parse(@"{
  ""Stores"": [
    ""Lambton Quay"",
    ""Willis Street""
  ],
  ""Manufacturers"": [
    {
      ""Name"": ""Acme Co"",
      ""Products"": [
        {
          ""Name"": ""Anvil"",
          ""Price"": 50
        }
      ]
    },
    {
      ""Name"": ""Contoso"",
      ""Products"": [
        {
          ""Name"": ""Elbow Grease"",
          ""Price"": 99.95
        },
        {
          ""Name"": ""Headlight Fluid"",
          ""Price"": 4
        }
      ]
    }
  ]
}");

string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co

decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
// 50

string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease

See: Json.Net Select Token

This uses the JSON.NET Library.

Sign up to request clarification or add additional context in comments.

2 Comments

You are right this is JSON, however what library does that use in .NET> As it is not recognising the JObject, I will mark as correct.
@Jambo that uses the JSON.NET library. I updated the question with a link to it.
0

I would use the Regex in C# your expressions for that would be like this

MatchCollection matchCtrls = Regex.Matches(pageText, @"Th(.*)e", RegexOptions.Singleline);

If you don't know what the text is going to be specifically then probably something like this

MatchCollection matchCtrls = Regex.Matches(pageText, @"": \(.*)\", RegexOptions.Singleline);

1 Comment

using System.Text.RegularExpressions;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.