2

I've try to parse string like this:

"#1#Process#{some process|info}{some name|some info {child info|child info}}{some name|some info}"

in several messages and create string like this:

#1#Process#
-some process|info
-some name|some info
   -child info|child info
-some name|some info

I'm trying to use RegExp and following code:

using System;
using System.Collections;
using System.Text.RegularExpressions;

namespace prRegEXP
{
    class Program
    {
        static String st="";

        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // TODO: Implement Functionality Here
            var pattern = @"\{(.*?)\}";
            var query = "#1#Process#{some process|info}{some name|some info {child info|child info}}{some name|some info}";
            FindTree (pattern, query);
            Console.WriteLine(st);
            Console.WriteLine();
            Console.WriteLine("Press any key to continue . . . ");
            Console.ReadKey(true);
        }       


        private static void FindTree (String pattern, String query) {
            var matches = Regex.Matches(query, pattern);            
            foreach (Match m in matches) {
                st += m.Groups[1] + "\n";

                if (Regex.IsMatch(m.Groups[1].ToString(), @"\{(.*?)" )) {                   
                    FindTree (@"\{(.*?)", m.Groups[1].ToString());

                }    
            }
        }
    }
}

It's based on example solution I found and I want to create some message tree which take care about messages inside (like child info|child name). And there can be a lot of them.

I cannot figure out how to match child expressions and send it in recursive parameter. Has any idea or fix?

Community
  • 1
  • 1
Kaha
  • 171
  • 4
  • 14
  • 1
    +1 for the query string! – Alex Feb 26 '14 at 10:52
  • 1
    You could achieve that by using recursive regexp, but it'd require good understanding to work efficiently (and even if supported by language - retrieval of recursive capturing groups might be tricky). Without it - You can construct a 2-depth pattern for Your 2-depth examples, but it's MUCH easier to just write a proper tokenizer with depth-counter... – Vlad Feb 26 '14 at 11:33

1 Answers1

2

Writing a simple regex to support N-depth recursions would be impossible(?) or at least very difficult.

A much easier solution would be to just go trough the string char by char and insert indentation and newlines when a new message is found.

Something along the lines of this should work:

private static String FindTree(String query)
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    String indent = "";
    foreach (var ch in query)  {
        if (ch == '{') {
            sb.Append("\n");
            sb.Append(indent);
            sb.Append("- ");
            indent += "\t";
        } else if (ch == '}') {
            indent = indent.Substring(1);
        } else {
            sb.Append(ch);
        }
    }
    return sb.ToString();
}

The above code is not tested, nor am I well versed in C# so it might be full of errors, but it should illustrate the basic idea.

rvalvik
  • 1,559
  • 11
  • 15
  • Unfortunately I need solution with RE – Kaha Feb 26 '14 at 12:28
  • 1
    @Kaha, RE is not a good instrument to do complete parsing, though You can use it efficiently during parsing for tokenizing, e.g. something like `([{\|}]|[^{\|}]+)` (not sure on C# escapes) can improve approach suggested by *rvalvik* from char-by-char up into string-by-string mode. – Vlad Feb 26 '14 at 17:01
  • Thanks to all of you. We already had solution without RE, so I was wondering about using RE. – Kaha Feb 27 '14 at 03:13