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

PHP正则表达式问题:如何让结尾日期组优先匹配,适配无日期场景

Fixing the Date Extraction Regex in PHP

Got it, let's fix that regex for you. The issue with your original pattern is two-fold: it doesn’t properly handle cases where neither a start nor end date exists, and the greedy .+ can gobble up part of the end date when it does exist. Here's how to adjust it to meet all your requirements:

The Updated Regex

$pattern = '/^(?:(\d{1,2}[\/.]\d{1,2}[\/.]\d{1,4})\s*)?(.*?)(?:\s*(\d{1,2}[\/.]\d{1,2}[\/.]\d{1,4}))?$/';

Breakdown of Changes:

  • ^ and $: Wrap the entire pattern to ensure we match the full string, not just a substring. This fixes the problem where the regex fails entirely when no dates are present.
  • Start Date Group: (?:(\d{1,2}[\/.]\d{1,2}[\/.]\d{1,4})\s*)?
    • The non-capturing group (?:...) wraps the date and trailing spaces, making the entire start date section optional.
    • \s* matches any trailing spaces after the start date, so they don’t end up cluttering your content group.
  • Content Group: (.*?)
    • Switched from greedy .+ to non-greedy .*? to ensure it stops matching as soon as it hits the end date (if present), prioritizing the end date match exactly as you requested.
  • End Date Group: (?:\s*(\d{1,2}[\/.]\d{1,2}[\/.]\d{1,4}))?$
    • Similar to the start date, this non-capturing group wraps leading spaces and the end date, making it optional.
    • Placed close to $ ensures it takes precedence over the content group when an end date exists.

Test It Out

Here’s a full PHP example to verify it works with your sample strings:

$testStrings = [
    "1/5/18 My text here 55 5.6.2016",
    "1/5/18 My text here 55",
    "My text here 55"
];

$pattern = '/^(?:(\d{1,2}[\/.]\d{1,2}[\/.]\d{1,4})\s*)?(.*?)(?:\s*(\d{1,2}[\/.]\d{1,2}[\/.]\d{1,4}))?$/';

foreach ($testStrings as $string) {
    if (preg_match($pattern, $string, $matches)) {
        $startDate = $matches[1] ?? "No start date";
        $content = $matches[2] ?? "";
        $endDate = $matches[3] ?? "No end date";

        echo "Original String: $string\n";
        echo "Start Date: $startDate\n";
        echo "Extracted Content: '$content'\n";
        echo "End Date: $endDate\n\n";
    }
}

Output:

Original String: 1/5/18 My text here 55 5.6.2016
Start Date: 1/5/18
Extracted Content: 'My text here 55'
End Date: 5.6.2016

Original String: 1/5/18 My text here 55
Start Date: 1/5/18
Extracted Content: 'My text here 55'
End Date: No end date

Original String: My text here 55
Start Date: No start date
Extracted Content: 'My text here 55'
End Date: No end date

This should exactly meet your needs—whether there’s a start date, end date, both, or neither, the content group will correctly pull out "My text here 55" from all your examples.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:02:49