簡體   English   中英

正則表達式 c# - 在何處插入空格

[英]Regex c# - where to insert the blank space

我想允許用戶在空白中輸入年份,但我不知道將 \\s\\s\\s\\s 在以下表達式中的位置。 這是我需要做的一個例子:如果用戶插入 03-07-_____ 程序必須在每年的 7 月 3 日執行(如果用戶插入空白的日期、月份或年份或這三個中的兩個)

System.Text.RegularExpressions.Regex rdate =
    new System.Text.RegularExpressions.Regex(@"^((((0?[1-9]|[12]\d|3[01]|\s\s|\s\d)[\-](0?[13578]|1[02]|\s\s)[\-]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\-](0?[13456789]|1[012])[\-]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\-]0?2[\-]((1[6-9]|[2-9]\d)?\d{2}))|(29[\-]0?2[\-]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$");

有人可以幫助我嗎?(這個表達式甚至可以驗證閏年)

這是你要求的

System.Text.RegularExpressions.Regex rdate =
    new System.Text.RegularExpressions.Regex(@"^((((0?[1-9]|[12]\d|3[01]|\s\s|\s\d)[\-](0?[13578]|1[02]|\s\s)[\-]((1[6-9]|[2-9]\d)?\d{2}|\s\s\s\s))|((0?[1-9]|[12]\d|30)[\-](0?[13456789]|1[012])[\-]((1[6-9]|[2-9]\d)?\d{2}|\s\s\s\s))|((0?[1-9]|1\d|2[0-8])[\-]0?2[\-]((1[6-9]|[2-9]\d)?\d{2}|\s\s\s\s))|(29[\-]0?2[\-]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|\s\s\s\s)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$");

但我不確定這是你真正想要的。 正則表達式已經打好了補丁,我繼續說下去。 但它不僅是維護噩夢,它的行為也很奇怪。 它接受空天,但僅當月份是一月、三月、五月...或空時。 更少的代碼行並不總是更好。 我建議完全重寫它。 類似的東西:

    protected DateTime? getDateTimeFromParts(string day, string month, string year)
    {
        DateTime now = DateTime.Now;

        int iyear;
        if (string.IsNullOrWhiteSpace(year))
        {
            iyear = now.Year;
        }
        else
        {
            iyear = int.Parse(year);
            if (iyear >= 0 && iyear < 100) { iyear += 2000; }
        }

        int imonth;
        if (string.IsNullOrWhiteSpace(month))
        {
            imonth = now.Month;
        }
        else
        {
            imonth = int.Parse(month);
        }

        int iday;
        if (string.IsNullOrWhiteSpace(day))
        {
            iday = now.Day;
        }
        else
        {
            iday = int.Parse(day);
        }

        if (iyear <= DateTime.MaxValue.Year && iyear >= DateTime.MinValue.Year)
        {
            if (imonth >= 1 && imonth <= 12)
            {
                if (DateTime.DaysInMonth(iyear, imonth) >= iday && iday >= 1)
                    return new DateTime(iyear, imonth, iday);
            }
        }

        return null;
    }

    protected DateTime? getDateTime(string dateStr)
    {
        Regex r = new Regex(@"^(\d\d)(\d\d)((\d\d)?\d\d)$");
        Match m = r.Match(dateStr);

        if (m.Success)
        {
            return getDateTimeFromParts(m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value);
        }

        r = new Regex(@"^(\d?\d|\s\d|\s\s)[-](\d?\d|\s\s)[-]((\d\d)?\d\d|\s\s\s\s)$");
        m = r.Match(dateStr);

        if (m.Success)
        {
            return getDateTimeFromParts(m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value);
        }

        return null;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM