You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中识别两类日期范围格式的正则表达式技术问询

Got it, let's figure out the right regex for your C# date range parsing needs. You've got three specific formats to cover, so I've put together a regex that handles all of them, plus a breakdown of how it works and how to implement it in your code.

Regex to Match All Your Date Range Formats

First off, here's the complete regex that covers all three formats you mentioned:

^(?:(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}\s*-\s*(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4})|(?:(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2}\s*-\s*\d{1,2}\s*,\s*\d{4})$

Breakdown of the Regex

Let's split this into two main branches that target your different date range styles:

Branch 1: Matches mm/DD/YYYY - mm/DD/YYYY

  • (?:0[1-9]|1[0-2]): Catches valid 2-digit months (01-12)
  • \/: Escaped forward slash for the date separator
  • (?:0[1-9]|[12]\d|3[01]): Matches valid 2-digit days (01-31)
  • \/\d{4}: Grabs the 4-digit year
  • \s*-\s*: Handles the hyphen between dates, with optional whitespace around it (works for both - and -)

Branch 2: Matches January 1-31,2018 and January 1 - 31, 2018

  • (January|February|March|April|May|June|July|August|September|October|November|December): Matches full month names
  • \s+\d{1,2}: Captures the start day (1 or 2 digits) with one or more spaces after the month
  • \s*-\s*: Handles the hyphen between days, with optional whitespace (supports both - and -)
  • \d{1,2}: Grabs the end day (1 or 2 digits)
  • \s*,\s*\d{4}: Matches the comma and 4-digit year, with optional whitespace around the comma (works for both ,2018 and , 2018)

How to Use This in C#

Here's a quick example of testing if a string matches this regex in your C# code:

using System;
using System.Text.RegularExpressions;

class DateRangeMatcher
{
    static void Main()
    {
        // Use a verbatim string to avoid escaping slashes
        string dateRangePattern = @"^(?:(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}\s*-\s*(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4})|(?:(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2}\s*-\s*\d{1,2}\s*,\s*\d{4})$";
        
        // Test all your target formats (plus a few edge cases)
        string[] testRanges = {
            "01/01/2018 - 01/31/2018",
            "January 1-31,2018",
            "February 1 - 28, 2018",
            "12/31/2023 - 01/01/2024",
            "March 5-10,2022",
            "April 15 - 20, 2021"
        };
        
        foreach (var range in testRanges)
        {
            bool isMatch = Regex.IsMatch(range, dateRangePattern);
            Console.WriteLine($"\"{range}\" is a valid format: {isMatch}");
        }
    }
}

Quick Notes

  • This regex is case-sensitive for month names (so january won't match). If you need case-insensitive matching, add the RegexOptions.IgnoreCase flag when calling Regex.IsMatch.
  • Regex doesn't validate impossible dates like 02/30/2018 (February 30). If you need to confirm the dates are actually valid, parse the matched parts into DateTime objects in C# after matching.

内容的提问来源于stack exchange,提问作者Pradeep Venishetti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:57:21