0

I have json string and i am trying to find value of key formId using regex. In json i have two formids. Regex is returning me two groups but second group is duplicate of first group.

My c# code is:

string json = System.IO.File.ReadAllText("data.json");
string regex = "(?<=\"formId\": \")(.*)(?=\")";

Match match = Regex.Match(json.ToLower(), regex,RegexOptions.IgnoreCase);

foreach (var item in match.Groups)
{
    Console.WriteLine(item.ToString());
}
Console.ReadLine();

Json string is

{
    "StepState": {
        "id": "f0f0e0c8-b4c7-45ff-b72d-58191d43a01f",
        "steps": [
            {
                "stepContent": {
                    "context": {
                        "formId": "af677fcd-4a34-42ed-acc1-91ded7734167",
                    }
                }
            },
            {
                "stepContent": {
                    "context": {
                        "formId": "3f6242a5-ff80-4f35-8589-d81bb545700e",
                    }
                }
            }
        ]
    }   
}

What am i missing? Is there any issue in regex?

Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • 5
    Does this answer your question? [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – ctwheels May 27 '20 at 15:01
  • 5
    Don't parse JSON with regex. Use Json.NET – ctwheels May 27 '20 at 15:02
  • @jamesMakinde why reinvent the wheel? Why chisel a rock with a sheet of paper? – ctwheels May 27 '20 at 15:05
  • @ctwheels if he has a reason for using regex, then let him do that. Not all problems can be solved with json.net, and the wheel does not solve every problem. – james Makinde May 27 '20 at 15:06
  • @jamesMakinde you're right, but as this question stands, there's no reason not to use Json.NET. Right now he's chiseling a rock with paper to make a wheel. Unless there's a specific requirement that the OP has to recreate the wheel, it's just a waste of time and prone to error, especially if you're not using the correct tools. We often see [tag:regex] tags on questions that can be done other ways. Regex isn't always the go-to and should not be. He's better off creating a parser, it's more efficient. – ctwheels May 27 '20 at 15:07
  • You are evaluating `json.ToLower()`, but have the non-lower-case `formId` in your pattern. Otherwise, it looks like it should work – Flydog57 May 27 '20 at 15:09
  • I don’t want parse it using regex. Don’t want to use any other stuff. toLower was used to validate ignore case option. If my reflex is correct then why are we getting duplicate groups? – user2614389 May 27 '20 at 15:19
  • Also my json is not always structured as shown above. Currently it is three levels down but it can change – user2614389 May 27 '20 at 15:26
  • I was about to post this as an answer, but this works for me: `string pattern = "(?<=\"formId\": \")(.*)(?=\")"; var regex = new Regex(pattern); var matches = regex.Matches(json);` – Flydog57 May 27 '20 at 15:31
  • using Matches works for me. Thanks you all for quick response. @Flydog57 can you put is as answer so that i can mark it as answer – user2614389 May 27 '20 at 16:49
  • The question is closed and will likely not get re-opened. In the future, if you want to ask a question where the obvious solution is "use a JSON parser" (or something else of the same style) and you have reasons why you don't want to use the obvious answer, you should include a paragraph that says "Yes, I know the obvious way to do this XYZ, but I don't want to do this because..." Don't worry, I always get confused with Match and Matches and Groups. Generally when I work with Regex, I write a test program and use it to debug my solution. – Flydog57 May 27 '20 at 18:23

0 Answers0