You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

寻求Python正则表达式匹配特定格式日期的实现方案

Solution for Matching Specific Date Formats in Python

Got it, let's work through this date extraction problem together. You've got 7 specific valid date formats to target, so we can build a regex that covers each one with alternations, while avoiding partial matches or obvious invalid values like months >12 or days >31 (note: regex can't handle edge cases like February 30th—we'd need Python's datetime module for that level of strict validation).

The Regex Pattern

Here's a regex that matches all your specified formats (line breaks are for readability; you can remove them for use in code):

\b(?:
    # MM/DD/YY or MM/DD/YYYY
    (?:0?[1-9]|1[0-2])/(?:0?[1-9]|[12]\d|3[01])/(?:\d{2}|\d{4})
    |
    # MM-DD-YYYY (with or without leading zero on month)
    (?:0?[1-9]|1[0-2])-(?:0?[1-9]|[12]\d|3[01])-\d{4}
    |
    # DD.MM.YYYY or DD. MM. YYYY (with optional spaces after dots)
    (?:0?[1-9]|[12]\d|3[01])\.(?:\s*0?[1-9]|1[0-2])\.\s*\d{4}
    |
    # YYYY-MM-DD
    \d{4}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[12]\d|3[01])
)\b

Breakdown of Each Section

Let's break down what each part does:

  • \b: Word boundary to prevent partial matches (e.g., avoiding 123/45/678 being incorrectly picked up as a date)
  • MM/DD/YY/YYYY:
    • (?:0?[1-9]|1[0-2]): Matches months 1-12, with or without a leading zero (e.g., 3 or 03)
    • (?:0?[1-9]|[12]\d|3[01]): Matches days 1-31, with or without a leading zero
    • (?:\d{2}|\d{4}): Matches 2-digit (e.g., 18) or 4-digit (e.g., 2018) years
  • MM-DD-YYYY: Same month/day logic as above, but using - as the separator, with a fixed 4-digit year
  • DD.MM.YYYY / DD. MM. YYYY: Swapped day/month order, using . as the separator (with optional spaces after dots to cover both formats)
  • YYYY-MM-DD: 4-digit year first, followed by month and day with - separators

Python Implementation Example

Here's how you can use this regex in your code to extract the valid dates:

import re

# Your test string
text_string = 'Examples for valid dates include "3/30/18", "3/30/2018", "3-30-2018", "03-30-2018", "30.3.2018", "30. 3. 2018", "2018-03-30". Some invalid ones: "13/30/2018", "3/32/2018", "2018/03/30"'

# Compile the regex (optional but efficient for repeated use)
date_regex = re.compile(r'\b(?:(?:0?[1-9]|1[0-2])/(?:0?[1-9]|[12]\d|3[01])/(?:\d{2}|\d{4})|(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[12]\d|3[01])-\d{4}|(?:0?[1-9]|[12]\d|3[01])\.(?:\s*0?[1-9]|1[0-2])\.\s*\d{4}|\d{4}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[12]\d|3[01]))\b')

# Extract all matches
valid_dates = date_regex.findall(text_string)

# Print results
print("Extracted valid dates:")
for date in valid_dates:
    print(f"- {date}")

Output

Extracted valid dates:
- 3/30/18
- 3/30/2018
- 3-30-2018
- 03-30-2018
- 30.3.2018
- 30. 3. 2018
- 2018-03-30

Extra Tip for Strict Calendar Validation

If you need to ensure the dates are actually calendar-valid (e.g., no February 30th), you can take the extracted strings and parse them with Python's datetime module:

from datetime import datetime

def is_valid_calendar_date(date_str):
    # Try all your formats to see if any parse successfully
    formats = ['%m/%d/%y', '%m/%d/%Y', '%m-%d-%Y', '%d.%m.%Y', '%d. %m. %Y', '%Y-%m-%d']
    for fmt in formats:
        try:
            datetime.strptime(date_str, fmt)
            return True
        except ValueError:
            continue
    return False

# Filter matches to only calendar-valid dates
strictly_valid_dates = [date for date in valid_dates if is_valid_calendar_date(date)]

内容的提问来源于stack exchange,提问作者Tiffany Tseng

火山引擎 最新活动