extracting data from a string with certain pattern

Hello everyone
As the topic suggests, I’m trying to extract data from a string. The string looks like this:
{“data1”, “data2”, “data3”, “data4”, “data5” ,“data6”}\n

From this sample, I would like to extract “data3” and “data4” and put them into 2 int respectively (the \n is a new line character).

The content and length for each “data slots” is dynamic, which means I cannot extract them by locating the positions of these data in the string. Please be noted that I have no control on the format of the string.

I believe using regular expression can solve the problem, but I have absolute zero knowledge about it. So I would like to ask for any ideas that can do the work.

Any help would be greatly appreciated.

Hmmm i would do it with String.Trim… like this:

        string t = "{\"data1\", \"data2\", \"data3\", \"data4\", \"data5\" ,\"data6\"}\n{\"data1\", \"data2\", \"data3\", \"data4\", \"data5\" ,\"data6\"}\n{\"data1\", \"data2\", \"data3\", \"data4\", \"data5\" ,\"data6\"}\n";
        t = t.TrimEnd('\n');

        foreach (string line in t.Split('\n'))
        {
            string[] fields = line.Trim('{', '}').Split(',');

            for (int i = 0; i < fields.Length; i++)
            {
                Debug.Log("Field#" + i + "=" + fields[i].Trim('"', ' '));
            }
        }

Thank you for your answer first as I will leave my PC soon so I’m not able to test it, but this looks promising! :smile:

I would like to know that

foreach (string line in t.Split('\n'))

the t.Split(‘\n’) is for multiple strings with the same format all stick together?

string.Split is great stuff:

Hey Aeronaut, thank you for your answer, that piece of code work perfectly fine!
But I would like to ask one more question, why wouldn’t it be working if I also trim these 2 characters:'"', ' '
in

string[] field = line.Trim('{', '}').Split(',');

Yes, like in the example…

It would be work too, but you will have a empty Space after and " before and after the field…