[研究]ASP.NET,WebForm,正則表達式,判斷是否符合「西洋年/月/日」或「西洋年/月」日期格式
2024-03-23
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19
********************************************************************************
C# 正則表達式,判斷是否符合YYYY/MM/DD,其中 YYYY 是 4位數西洋年, MM 是2位數月份,D是2位數日期
Default2.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="符合者:"></asp:Label>
</form>
</body>
</html>
|
Default.aspx.cs
using System;
using System.Text.RegularExpressions;
namespace WebApplication2
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] dates = {
"2024/03/19",
"2024/3/19",
"2024/03/9",
"2024/3/9",
"2024/03",
"2024/3",
"2024/03/0",
"2024/0",
"2024/0/19",
"2024/03/0"
}; //是否符合YYYY/MM/DD string pattern = @"^\d{4}/(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|3[0 -1])$";
foreach (string date in dates)
{
//Console.WriteLine($"{date}: {Regex.IsMatch(date, pattern)}");
if (Regex.IsMatch(date, pattern))
Label1.Text = Label1.Text + date + "<br />";
}
}
//---
}
}
|
********************************************************************************
C# 正則表達式,判斷是否符合
YYYY/MM/DD 或 YYYY/M/D 或 YYYY/MM/D 或 YYYY/M/DD 或 YYYY/MM 或 YYYY/M 或 YYYY/0M/0D 或 YYYY/MM/0D 或 YYYY/0M/DD,其中 YYYY 是 4位數西洋年,M 或 MM 是月份,D 或 D 是日期
Default4.aspx.cs
using System;
using System.Text.RegularExpressions;
namespace WebApplication1
{
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] dates = {
"2024/03/19",
"2024/3/19",
"2024/03/9",
"2024/3/9",
"2024/03",
"2024/3",
"2024/03/0",
"2024/03/19",
"2024/03/0",
"2024/03/19",
"2024/0",
"2024/03/19",
"2024/0/19",
"2024/03/19",
"2024/0/0",
"2024/03/0"
};
string pattern = @"^(\d{4})/(0?[1-9]|1[0-2])/(0?[1-9]|1\d|2[0-9]|3[01])$";
foreach (string date in dates)
{
//Console.WriteLine($"{date}: {Regex.IsMatch(date, pattern)}");
if (Regex.IsMatch(date, pattern))
Label1.Text = Label1.Text + date + "<br />";
}
}
}
} |
這個範例程式碼使用了正規表示式^(\d{4})/(0?[1-9]|1[0-2])/(0?[1-9]|1\d|2[0 -9]|3[01])$ 來符合日期字串。 其中:
- ^ 表示字串的開始。
- \d{4} 以四位數字表示表示的年份。
- / 匹配斜線字元。
- (0?[1-9]|1[0-2]) 配對月份,可以是1位或2位數字,1-12之間。
- (0?[1-9]|1\d|2[0-9]|3[01]) 匹配日期,可以是1位或2位數字,根據月份不同,允許的範圍為1-31之 間。
- $ 表示字串的結尾。
實際測試
********************************************************************************
C# 正則表達式,判斷是否符合
YYYY/MM/DD 或 YYYY/M/D 或 YYYY/MM/D 或 YYYY/M/DD 或 YYYY/MM 或 YYYY/M 或 YYYY/0M/0D 或 YYYY/MM/0D 或 YYYY/0M/DD 或 YYYY/MM 或 YYYY/0M,
其中 YYYY 是 4位數西洋年,M 或 MM 是月份,D 或 D 是日期
string pattern = @"^\d{4}/(0?[1-9]|1[0-2])(/(0?[1-9]|[12]\d|3[01]))?$";
********************************************************************************
提取日期部分並顯示。
SELECT CAST(YourDateTimeColumn AS DATE) AS DatePart FROM YourTable;
或
SELECT CONVERT(DATE, YourDateTimeColumn) AS DatePart FROM YourTable;
(完)
相關


沒有留言:
張貼留言