コード例 #1
0
        /// <summary>
        /// Capture index number 0 is the entire match. Capture
        /// index 1 is the first matched group from the left, and so on.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="regularExpression">The regular expression.</param>
        /// <param name="captureIndex">Index of the capture.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static string Extract(string input, string regularExpression, int captureIndex, RegexOptions options)
        {
            Match m = Regex.Match(input, regularExpression, options);

            if (m.Success)
            {
                return(RegexUtilities.GetCapture(m, captureIndex));
            }
            return(null);
        }
コード例 #2
0
        private static bool DoParse(string str, TzTimeZone localTimeZone, out TzDateTime result)
        {
            result = null;

            if (string.IsNullOrEmpty(str))
            {
                throw new ArgumentNullException("str");
            }

            Match m;
            int   matchIndex = RegexUtilities.MatchAny(str, out m,
                                                       FormatYear,
                                                       FormatYearAndMonth,
                                                       FormatComplete,
                                                       FormatCompleteHM,
                                                       FormatCompleteHMS,
                                                       FormatCompleteHMSF);
            int          year = 0, month = 1, day = 1, hours = 0, minutes = 0, seconds = 0, millisecond = 0;
            DateTimeKind kind     = DateTimeKind.Local;
            TzTimeZone   timezone = localTimeZone;

            if (matchIndex != -1)
            {
                if (matchIndex >= 0)
                {
                    year = int.Parse(RegexUtilities.GetCapture(m, 1));
                }
                if (matchIndex >= 1)
                {
                    month = int.Parse(RegexUtilities.GetCapture(m, 2));
                }
                if (matchIndex >= 2)
                {
                    day = int.Parse(RegexUtilities.GetCapture(m, 3));
                }
                if (matchIndex >= 3)
                {
                    hours   = int.Parse(RegexUtilities.GetCapture(m, 4));
                    minutes = int.Parse(RegexUtilities.GetCapture(m, 5));

                    // At this level, we also expect a time zone designator
                    kind = DateTimeKind.Utc;
                    string tzd = RegexUtilities.GetLastCapture(m, 2);
                    if (tzd == UtcZuluIdentifier.ToString())
                    {
                        timezone = TzTimeZone.ZoneUTC;
                    }
                    else
                    {
                        timezone = TzTimeZone.GetTimeZoneByOffset(RegexUtilities.GetLastCapture(m, 1));
                    }
                }
                if (matchIndex >= 4)
                {
                    seconds = int.Parse(RegexUtilities.GetCapture(m, 6));
                }
                if (matchIndex >= 5)
                {
                    millisecond = int.Parse(RegexUtilities.GetCapture(m, 7));
                }
                result = new TzDateTime(year, month, day, hours, minutes, seconds, millisecond, kind, timezone);
                return(true);
            }
            return(false);
        }