c# - Complex string splitting -


i have string following:

[testing.user]|info:([testing.info]|name:([system.string]|matt)|age:([system.int32]|21))|description:([system.string]|this description) 

you can @ tree:

- [testing.user] - info         - [testing.info]         - name                 - [system.string]                 - matt         - age                 - [system.int32]                 - 21 - description         - [system.string]         - description 

as can see, it's string serialization / representation of class testing.user

i want able split , following elements in resulting array:

 [0] = [testing.user]  [1] = info:([testing.info]|name:([system.string]|matt)|age:([system.int32]|21))  [2] = description:([system.string]|this description) 

i can't split | because result in:

 [0] = [testing.user]  [1] = info:([testing.info]  [2] = name:([system.string]  [3] = matt)  [4] = age:([system.int32]  [5] = 21))  [6] = description:([system.string]  [7] = description) 

how can expected result?

i'm not regular expressions, aware possible solution case.

there more enough splitting answers already, here approach. if input represents tree structure, why not parse tree? following code automatically translated vb.net, should work far tested it.

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks;  namespace treeparse {     class program     {         static void main(string[] args)         {             var input = "[testing.user]|info:([testing.info]|name:([system.string]|matt)|age:([system.int32]|21))|description:([system.string]|this description)";             var t = stringtree.parse(input);             console.writeline(t.tostring());             console.readkey();         }     }      public class stringtree     {         //branching constants         const string branchoff = "(";         const string branchback = ")";         const string nexttwig = "|";          //content of twig         public string text;         //list of sub-twigs         public list<stringtree> twigs;         [system.diagnostics.debuggerstepthrough()]         public stringtree()         {             text = "";             twigs = new list<stringtree>();         }          private static void parserecursive(stringtree tree, string inputstr, ref int position)         {             {                 stringtree newtwig = new stringtree();                 {                     newtwig.text = newtwig.text + inputstr[position];                     position += 1;                 } while (!(position == inputstr.length || (new string[] { branchback, branchoff, nexttwig }.tolist().contains(inputstr[position].tostring()))));                 tree.twigs.add(newtwig);                 if (position < inputstr.length && inputstr[position].tostring() == branchoff) { position += 1; parserecursive(newtwig, inputstr, ref position); position += 1; }                 if (position < inputstr.length && inputstr[position].tostring() == branchback)                     break; // todo: might not correct. : exit                 position += 1;             } while (!(position >= inputstr.length || inputstr[position].tostring() == branchback));         }          /// <summary>         /// call parse input stringtree objects using recursion         /// </summary>         public static stringtree parse(string input)         {             stringtree t = new stringtree();             t.text = "root";             int start = 0;             parserecursive(t, input, ref start);             return t;         }          private void tostringrecursive(ref stringbuilder sb, stringtree tree, int level)         {             (int = 1; <= level; i++)             {                 sb.append("   ");             }             sb.appendline(tree.text);             int nextlevel = level + 1;             foreach (stringtree nexttree in tree.twigs)             {                 tostringrecursive(ref sb, nexttree, nextlevel);             }         }          public override string tostring()         {             var sb = new system.text.stringbuilder();             tostringrecursive(ref sb, this, 0);             return sb.tostring();         }      } } 

result (click):

you values of each node associated subvalues in treelike structure , can whatever like, example show structure in treeview control:

enter image description here


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -