Files
chapter-organizer/Core/Parsers/EventOccurrence/TimePatterns.cs
T
poprhythm f916cfad6b Refactor event occurrence parsing by introducing modular components for improved maintainability
This commit restructures the EventOccurrenceParser by breaking down its functionality into modular components, including EventDefinitionResolver, LineClassifier, LocationPatternMatcher, SectionHeaderMatcher, TimeLocationParser, and TimeParser. This refactoring enhances code readability and maintainability, allowing for easier updates and testing. Additionally, the TextUtil class has been updated to include input sanitization methods. Comprehensive unit tests have been added to ensure the correctness of the new parsing logic and to validate the handling of various event occurrence scenarios.
2026-01-08 20:23:57 -05:00

44 lines
1.6 KiB
C#

namespace Core.Parsers.EventOccurrence;
/// <summary>
/// Shared regex patterns for time parsing.
/// </summary>
internal static class TimePatterns
{
/// <summary>
/// AM/PM pattern (case-insensitive via IgnoreCase flag).
/// Matches: "a.m.", "am", "A.M.", "AM", "p.m.", "pm", "P.M.", "PM"
/// </summary>
public const string AmPm = @"(?:a|p)\.?m\.?";
/// <summary>
/// Hour pattern: matches 1-2 digits (1-12 or 1-23).
/// </summary>
public const string Hour = @"\d{1,2}";
/// <summary>
/// Minute pattern with named group for parsing: matches exactly 2 digits if present.
/// Used when parsing to TimeOnly objects where minutes must be valid.
/// </summary>
public const string MinuteWithGroup = @"(?<Minute>\d{2})?";
/// <summary>
/// Minute pattern for matching: matches 0-2 digits.
/// Used when extracting time strings (more lenient for location parsing).
/// </summary>
public const string MinuteFlexible = @"\d{0,2}";
/// <summary>
/// Time value pattern: matches either NOON or a time with AM/PM.
/// Used for matching time strings in location parsing (more lenient minute format).
/// </summary>
public static string TimeValue => $@"(?:NOON|{Hour}:?{MinuteFlexible}\s*{AmPm})";
/// <summary>
/// Time pattern with named groups for parsing hour, minute, and AM/PM.
/// Used when parsing to TimeOnly objects (requires 2-digit minutes if present).
/// </summary>
public static string TimeWithGroups => $@"(?<Hour>{Hour}):?{MinuteWithGroup}\s?(?<APM>{AmPm})";
}