Jay Stewart
Jay Stewart

Reputation: 25

Split a non delimited, and variable length string

given the following string

38051211)JLRx(0x04>0x01):JL_PAGE_INFO(0x63,0x00,0x01,0x03,0x00,0x73,0x00,0x00,0x0A,0x01,0x01,0xF2,0x01)

How can I split it so I can use each split in a listview column?

I can use split at the : for example but then I need to split at the next ( and each value after split using the ,.

Any advice would be greatly appreciated, its how I add the 2nd and 3rd and so on parts I am struggling with

 {
    if (line.Contains("JLTx"))
    {
        string[] JLTx = line.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
        listView1.Items.Add(JLTx[0]);
        listView1.Items[listView1.Items.Count - 1].SubItems.Add(JLTx[1]);                       
    }
}

So using the following regex

Regex regex = new Regex(@"(.*)JLTx\((.*)\):(JL_[(A-Z)_]*)\((.*)\)");

I cant seem to split at the : as not in any of the matches. Where am I going wrong

Thanks all

Upvotes: 0

Views: 163

Answers (2)

MikeJ
MikeJ

Reputation: 1379

As others have pointed out you have a lot of options for parsing this into some header friendly format. @Johns answer above would work if your JL_PAGE_INFO is stable for all input. You could also use a regex. A lot of it depends on how stable your input data is. Here is a example using string functions to create the list of headers you described.

    static IEnumerable<string> Tokenize(string input)
    {
        if (string.IsNullOrEmpty(input))
            yield break;
        if (')' != input[input.Length - 1])
            yield break;

        int colon = input.IndexOf(':');

        string pageData = input.Substring(colon + 1);

        if (string.IsNullOrEmpty(pageData))
            yield break;

        int open = pageData.IndexOf('(');

        if (colon != -1 && open != -1)
        {
            yield return input.Substring(0, colon+1);

            foreach (var token in pageData.Substring(open+1, pageData.Length - (open + 1) - 1).Split(','))
                yield return token;
        }
    }

Upvotes: 1

John
John

Reputation: 829

If the second item of your split string - JLTx[1] - is always going to be JL_PAGE_INFO(...) I would try this:

string[] mystring = JLTx[1].Replace("JL_PAGE_INFO(","").Replace(")","")Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

Upvotes: 1

Related Questions