Generic delimited parsing algorithm

Hi all,

You can use the following extension method to parse any string that is split by a delimiter. It will take care of most issues associated with splitting strings (quotes, etc) and works very well in almost every scenario I have used it in.

Good one for the library of extension methods, definitely.

        /// <summary>

        /// This method will attepmt to parse the string into its delimited parts, taking a delimiter string as a parameter.

        /// </summary>

        /// <param name="strLine">The string to parse.</param>

        /// <param name="fieldDelimiter">The string used as a delimiter in the parse string.</param>

        /// <returns></returns>

        public static IEnumerable<string> ParseDelimited(this string strLine, string fieldDelimiter)

        {

            string separatorEscaped = Regex.Escape(fieldDelimiter);

            string regularExpression = @"^(?:""(?<item>[^""]*)""|(?<item>[^{0}]*))(?:{0}(?:""(?<item>[^""]*)""|(?<item>[^{0}]*)))*$";

 

            var regex = new Regex(string.Format(regularExpression, separatorEscaped));

 

            var split = regex

                .Match(strLine)

              .Groups["item"]

              .Captures

              .Cast<Capture>()

              .Select(c => c.Value)

              .ToArray();

 

            return split;

        }