如何在 C# 中解析没有空格的字符串中的日期?

How to parse date in string without spaces in C#?

试过此代码,它仅解析 01/03/2022,由于某种原因它不适用于 26/03/2022

        string RegexDateTimeLicenseTemplate = @"\b([0-9]{2})[./-]([0-9]{2})[./-]([0-9]{4}|[0-9]{2})";
        var dateRegexTemplate = new Regex(RegexDateTimeLicenseTemplate);
        List<DateTime> dateTimes;
        string[] formats = { "dd/MM/yyyy"};
        plainText = @"ףמ-01/03/2022עד26/03/2022המטו";

        dateTimes = dateRegexTemplate.Matches(plainText).Select(x => DateTime.ParseExact(x.Value,
            formats,
            CultureInfo.InvariantCulture)).ToList();

从我在示例中看到的情况来看,日期实际上可能被单词字符包围,因此不应存在单词边界。此外,您的模式应允许在日或月中使用 1 或 2 位数字。综合起来,我建议采用以下模式:

([0-9]{1,2})[./-]([0-9]{1,2})[./-]([0-9]{4}|[0-9]{2})