示例#1
0
        private static PreParsedInput PreParseInput(string inputStr)
        {
            var   result = new PreParsedInput();
            Match matchSingleCharacterDelimeter = INPUT_PARSING_REGEXP_SINGLE_CHAR_DELIMETER.Match(inputStr);

            if (matchSingleCharacterDelimeter.Success)
            {
                result.CustomDelimeters = new string[] { matchSingleCharacterDelimeter.Groups[DELIMETER_GROUP_NAME].Value };
                result.NumbersString    = matchSingleCharacterDelimeter.Groups[INPUT_NUMBERS_GROUP_NAME].Value;
            }
            else
            {
                Match matchManyCharacterDelimeter = INPUT_PARSING_REGEXP_MANY_DELIMETERS.Match(inputStr);
                if (matchManyCharacterDelimeter.Success)
                {
                    // string with a list of delimeters in a format "[{delimiter1}][{delimiter2}]..[{delimiterN}]"
                    string delimetersString = matchManyCharacterDelimeter.Groups[DELIMETER_GROUP_NAME].Value;
                    // split it to individual delimeters
                    // option StringSplitOptions.RemoveEmptyEntries is used to remove empty strings
                    string[] delimeters = delimetersString.Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);

                    result.CustomDelimeters = delimeters;
                    result.NumbersString    = matchManyCharacterDelimeter.Groups[INPUT_NUMBERS_GROUP_NAME].Value;
                }
                else
                {
                    result.NumbersString = inputStr;
                }
            }
            return(result);
        }
示例#2
0
        public IEnumerable <int> ParseNumbers(string inputStr)
        {
            // if input value is null or empty return empty collection
            if (string.IsNullOrWhiteSpace(inputStr))
            {
                return(new List <int>());
            }

            PreParsedInput parsedInput = PreParseInput(inputStr);

            string[] delimeters = GetAllDelimeters(parsedInput.CustomDelimeters);

            return(parsedInput.NumbersString
                   .Split(delimeters, StringSplitOptions.None)
                   .Select(x => ConvertToNumber(x)));
        }