Is there an easy way to parse a structured text file in C #?

I am trying to parse a text file, but I feel that there should be a better way to do this.

A text file consists of elements that can contain both key value pairs and other elements. I believe that the file will only reach the element of the grandson (for example, the element inside the element, inside the element).

I am currently repeating the file line by line and keeping track of the depth at which I am. Then, as soon as I get to the termination character ('}'), and I'm at level 0, I know that I have captured the whole parent element.

Since I don’t think I explained it well, my file structure will look something like this.

parent_item {
key: value
key: value
child_item {
    key: value
    key: value
}
child_item {
    key: value
    key: value
    key: value
    grandchild_item {
        key: value
        key: value
    }
}
key: value
key: value
}
parent_item {
    key: value
    key: value
    child_item {
        key: value
        key: value
    }
}

0 , , - , 3 ( → → ).

, ?

+4
5

JSON, , . , , JSON-, JSON.NET.

, , . , { , }.

+3

JSON, ( curly '{' curly } ), JSON . , , - :

StringBuilder jsonEncoded = new StringBuilder();
string[] lines = content.Split("\n");
foreach(string line in lines)
{
    if(line.EndsWith("{") || line.EndsWith("}")
        jsonEncoded.AppendLine(line);
    else
        jsonEncoded.AppendLine(line + ",");
}

JSON Deseralization.

+1

, , . :

JSON

json. , , JSON.NET.

, . :

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static string Quoterize(string s)
    {
        return Regex.Replace(s, @"\w+", match => "\"" + match + "\"");
    }

    public static string RewriteThisPlease(string s)
    {
        return s
            .Replace("\n", "," + Environment.NewLine)
            .Replace(" ", "")
            .Replace(Environment.NewLine, "")
            .Replace("{,", "{")
            .Replace(",}", "}");
    }

    public static void Main()
    {
        var k = @""; // your file goes here
        Console.WriteLine("{"+MoveToRegexPlease(Quoterize((k).Replace("{", ": {")))+"}");
    }
}

YAML

json. . yaml :

// where k is your file as string
Console.WriteLine(k.Replace("{\n", ":\n").Replace("}",""));

json. JSON, , .

json. JSON- . .

!

+1

( ) RegEx

. , :

static void Main(string[] args)
{
    // here I read your posted file
    string input = System.IO.File.ReadAllText("test.txt");

    input = input.Replace('{', '<');
    input = input.Replace('}', '>');

    string pattern = "^[^<>]*" +
              "(" +
              "((?'Open'<)[^<>]*)+" +
              "((?'Close-Open'>)[^<>]*)+" +
              ")*" +
              "(?(Open)(?!))$";

    //string input = "<abc><mno<xyz>>";

    Match m = Regex.Match(input, pattern);
    if (m.Success == true)
    {
        Console.WriteLine("Input: \"{0}\" \nMatch: \"{1}\"", input, m);
        int grpCtr = 0;
        foreach (Group grp in m.Groups)
        {
            Console.WriteLine("   Group {0}: {1}", grpCtr, grp.Value);
            grpCtr++;
            int capCtr = 0;
            foreach (Capture cap in grp.Captures)
            {
                Console.WriteLine("      Capture {0}: {1}", capCtr, cap.Value);
                capCtr++;
            }
        }
    }
    else
    {
        Console.WriteLine("Match failed.");
    }   

    Console.ReadKey();
}

, . , , .

0

Source: https://habr.com/ru/post/1671054/


All Articles