示例#1
0
        public (DateTime?date, DateTime?time) Match(string value, DateTime seed)
        {
            DateTime?resultDate = null;
            DateTime?resultTime = null;
            var      match      = _regex.Match(value);

            if (!match.Success)
            {
                return(resultDate, resultTime);
            }

            var groupNames = _regex.GetGroupNames();

            int?dateMatchKey = GetKey(groupNames, match.Groups, "da");

            if (dateMatchKey.HasValue)
            {
                string[] dateMatchArgs = GetParams(match.Groups, "db" + dateMatchKey);
                resultDate = DatePattern[dateMatchKey.Value](seed, dateMatchArgs);
            }

            int?timeMatchKey = GetKey(groupNames, match.Groups, "ta");

            if (timeMatchKey.HasValue)
            {
                seed = (resultDate ?? seed).Date;
                string[] timeMatchArgs = GetParams(match.Groups, "tb" + timeMatchKey);
                resultTime = TimePattern[timeMatchKey.Value](seed, timeMatchArgs);
            }

            return(resultDate, resultTime);
        }
示例#2
0
        private string BuildRegex(PatternCollection pattern, char prefix)
        {
            StringBuilder sb = new StringBuilder();
            var           i  = 0;

            foreach (var key in pattern.Keys)
            {
                var key2 = key.Replace("(", $"(?<{prefix}b{i}>");
                sb.Append($"(?<{prefix}a{i}>{key2})|");
                i++;
            }

            return("(" + sb.ToString(0, sb.Length - 1) + ")");
        }