Monday, April 27, 2015

REgex split join within brackets

            string s1 = "Who [kavas,kar] the,se [kav,as] notes?";
            var myRegex = new Regex(@"\[[^\]]*\]|(,)");
            var group1Caps = new StringCollection();

            string replaced = myRegex.Replace(s1, delegate(Match m)
            {
                if (m.Groups[1].Value == "") return m.Groups[0].Value;
                else return "SPLIT_HERE";
            });

            string[] splits = Regex.Split(replaced, "SPLIT_HERE");
            ArrayList list = new ArrayList();
            foreach (string split in splits)
            {
                list.Add(split.Replace(",", "."));
            }

            string[] array = list.ToArray(typeof(string)) as string[];

            Console.WriteLine(string.Join(",", array));

            Console.WriteLine("\nPress Any Key to Exit.");
            Console.ReadKey();

No comments:

Post a Comment