/// <summary> /// Builds the RegEx that allows us to preParse a time span string. /// </summary> /// <returns>regular expression string</returns> private static string BuildParseRegularExpression() { StringBuilder regExPattern = new StringBuilder(); regExPattern.Append("^"); regExPattern.Append(TimeSpanInputBox.BuildUnitRegEx( "years", GetResourceString("YearsUnit"), GetResourceString("YearUnit"), GetResourceString("YearsLongUnit"), GetResourceString("YearLongUnit"))); regExPattern.Append("\\s*"); regExPattern.Append(TimeSpanInputBox.BuildUnitRegEx( "months", GetResourceString("MonthsUnit"), GetResourceString("MonthUnit"), GetResourceString("MonthsLongUnit"), GetResourceString("MonthLongUnit"))); regExPattern.Append("\\s*"); regExPattern.Append(TimeSpanInputBox.BuildUnitRegEx( "weeks", GetResourceString("WeeksUnit"), GetResourceString("WeekUnit"), GetResourceString("WeeksLongUnit"), GetResourceString("WeekLongUnit"))); regExPattern.Append("\\s*"); regExPattern.Append(TimeSpanInputBox.BuildUnitRegEx( "days", GetResourceString("DaysUnit"), GetResourceString("DayUnit"), GetResourceString("DaysLongUnit"), GetResourceString("DayLongUnit"))); regExPattern.Append("\\s*"); regExPattern.Append(TimeSpanInputBox.BuildUnitRegEx( "hours", GetResourceString("HoursUnit"), GetResourceString("HourUnit"), GetResourceString("HoursLongUnit"), GetResourceString("HourLongUnit"))); regExPattern.Append("\\s*"); regExPattern.Append(TimeSpanInputBox.BuildUnitRegEx( "minutes", GetResourceString("MinutesUnit"), GetResourceString("MinuteUnit"), GetResourceString("MinutesLongUnit"), GetResourceString("MinuteLongUnit"))); regExPattern.Append("\\s*"); regExPattern.Append(TimeSpanInputBox.BuildUnitRegEx( "seconds", GetResourceString("SecondsUnit"), GetResourceString("SecondUnit"), GetResourceString("SecondsLongUnit"), GetResourceString("SecondLongUnit"))); regExPattern.Append("$"); return(regExPattern.ToString()); }
/// <summary> /// Implements the extra parsing logic to recognise truncated forms of long TimeSpanUnits /// </summary> /// <param name="value">Input string to preparse for truncated TSUs</param> /// <param name="parsedValue">A string regularised to the long TimeSpanUnits version of the input string</param> /// <returns>status of update</returns> private static bool TryPreParseUnits(string value, out string parsedValue) { // NOT the same as the NhsTimeSpan version... string parseExpression = TimeSpanInputBox.BuildParseRegularExpression(); Regex regEx = new Regex(parseExpression, RegexOptions.IgnoreCase); Match wordMatch = regEx.Match(value); string returnValue = ""; parsedValue = null; long result; if (wordMatch != null) { // now convert to full length versions for easier parsing by NhsTimeSpan... if (wordMatch.Groups["years"] != null && string.IsNullOrEmpty(wordMatch.Groups["years"].Value) == false) { if (long.TryParse(wordMatch.Groups["years"].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) { returnValue += result + GetResourceString("YearsLongUnit") + " "; } else { return(false); } } if (wordMatch.Groups["months"] != null && string.IsNullOrEmpty(wordMatch.Groups["months"].Value) == false) { if (long.TryParse(wordMatch.Groups["months"].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) { returnValue += result + GetResourceString("MonthsLongUnit") + " "; } else { return(false); } } if (wordMatch.Groups["weeks"] != null && string.IsNullOrEmpty(wordMatch.Groups["weeks"].Value) == false) { if (long.TryParse(wordMatch.Groups["weeks"].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) { returnValue += result + GetResourceString("WeeksLongUnit") + " "; } else { return(false); } } if (wordMatch.Groups["days"] != null && string.IsNullOrEmpty(wordMatch.Groups["days"].Value) == false) { if (long.TryParse(wordMatch.Groups["days"].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) { returnValue += result + GetResourceString("DaysLongUnit") + " "; } else { return(false); } } if (wordMatch.Groups["hours"] != null && string.IsNullOrEmpty(wordMatch.Groups["hours"].Value) == false) { if (long.TryParse(wordMatch.Groups["hours"].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) { returnValue += result + GetResourceString("HoursLongUnit") + " "; } else { return(false); } } if (wordMatch.Groups["minutes"] != null && string.IsNullOrEmpty(wordMatch.Groups["minutes"].Value) == false) { if (long.TryParse(wordMatch.Groups["minutes"].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) { returnValue += result + GetResourceString("MinutesLongUnit") + " "; } else { return(false); } } if (wordMatch.Groups["seconds"] != null && string.IsNullOrEmpty(wordMatch.Groups["seconds"].Value) == false) { if (long.TryParse(wordMatch.Groups["seconds"].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) { returnValue += result + GetResourceString("SecondsLongUnit") + " "; } else { return(false); } } } if (returnValue != null && returnValue.Length > 0) { parsedValue = returnValue.TrimEnd(); return(true); } else { parsedValue = value; return(true); } }