符合有效日期的正则表达式符合有效日期的正则表达式符合有效日期的正则表达式符合有效日期的正则表达式
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
符合有效日期的正则表达式
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式范例 » 符合有效日期的正则表达式

范例
正则表达式范例
数字范围
浮点数
电子邮件地址
IP 地址
有效日期
数字日期转换为文本
信用卡号码
符合完整行
删除重复行
编程
两个相近的字词
陷阱
灾难性的回溯
过多重复
阻断服务
让所有内容都是选项
重复捕获组
混合 Unicode 和 8 比特
更多内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

符合有效日期的正则表达式

^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$ 符合从 1900-01-01 到 2099-12-31,且有四个分隔符号选择的 yyyy-mm-dd 格式日期。锚点可确保整个变量是一个日期,而不是包含日期的文本片段。年份由 (19|20)\d\d 符合。我使用 交替 来允许前两个数字为 19 或 20。括号是强制性的。如果我省略它们,正则表达式引擎会寻找 19 或正则表达式的其余部分,这会符合介于 2000-01-01 和 2099-12-31 之间的日期。括号是唯一能阻止垂直线将整个正则表达式分成两个选项的方法。

月份由 0[1-9]|1[012] 匹配,再次用括号包起来以保持两个选项在一起。通过使用 字符类别,第一个选项会匹配 01 到 09 之间的数字,而第二个选项会匹配 10、11 或 12。

正则表达式的最后一部分包含三个选项。第一个选项会匹配 01 到 09 之间的数字,第二个选项会匹配 10 到 29 之间的数字,而第三个选项会匹配 30 或 31。

巧妙地使用交替让我们可以排除无效的日期,例如 2000-00-00,而不用交替就无法排除。要做到真正完美,您必须将月份拆分成不同的选项,以考量月份的长度。上述正则表达式仍然会匹配 2003-02-31,而这并非有效的日期。让前导零为可选的可以是另一个增强功能。

如果您想要强制分隔符一致,可以使用 反向引用。 ^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$ 会匹配 1999-01-01,但不会匹配 1999/01-01。

同样地,您要让正则表达式多么复杂取决于您使用它的数据,以及如果出现不想要的匹配,问题会有多严重。如果您在脚本中验证用户的日期输入,在正则表达式之外运行某些检查可能比较容易。例如,当年份不是闰年时排除 2 月 29 日,在脚本语言中运行会容易得多。使用简单的算术来检查年份是否可被 4 整除(且不可被 100 整除,除非可被 400 整除),会比使用正则表达式容易得多。

以下是如何在 Perl 中检查有效的日期。我也加入了括号,将年份截取到反向引用中。

sub isvaliddate {
  my $input = shift;
  if ($input =~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$!) {
    # At this point, $1 holds the year, $2 the month and $3 the day of the date entered
    if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) {
      return 0; # 31st of a month with 30 days
    } elsif ($3 >= 30 and $2 == 2) {
      return 0; # February 30th or 31st
    } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 != 0 or $1 % 400 == 0))) {
      return 0; # February 29th outside a leap year
    } else {
      return 1; # Valid date
    }
  } else {
    return 0; # Not a date
  }
}

若要比对 mm/dd/yyyy 格式的日期,请将正则表达式重新排列为 ^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$。对于 dd-mm-yyyy 格式,请使用 ^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$。

符合有效日期的正規表示式
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式範例 » 符合有效日期的正規表示式

範例
正規表示式範例
數字範圍
浮點數
電子郵件地址
IP 位址
有效日期
數字日期轉換為文字
信用卡號碼
符合完整行
刪除重複行
程式設計
兩個相近的字詞
陷阱
災難性的回溯
過多重複
阻斷服務
讓所有內容都是選項
重複擷取群組
混合 Unicode 和 8 位元
本網站的更多資訊
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

符合有效日期的正規表示式

^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$ 符合從 1900-01-01 到 2099-12-31,且有四個分隔符號選擇的 yyyy-mm-dd 格式日期。錨點可確保整個變數是一個日期,而不是包含日期的文字片段。年份由 (19|20)\d\d 符合。我使用 交替 來允許前兩個數字為 19 或 20。括號是強制性的。如果我省略它們,正規表示式引擎會尋找 19 或正規表示式的其餘部分,這會符合介於 2000-01-01 和 2099-12-31 之間的日期。括號是唯一能阻止垂直線將整個正規表示式分成兩個選項的方法。

月份由 0[1-9]|1[012] 匹配,再次用括號包起來以保持兩個選項在一起。透過使用 字元類別,第一個選項會匹配 01 到 09 之間的數字,而第二個選項會匹配 10、11 或 12。

正規表示式的最後一部分包含三個選項。第一個選項會匹配 01 到 09 之間的數字,第二個選項會匹配 10 到 29 之間的數字,而第三個選項會匹配 30 或 31。

巧妙地使用交替讓我們可以排除無效的日期,例如 2000-00-00,而不用交替就無法排除。要做到真正完美,您必須將月份拆分成不同的選項,以考量月份的長度。上述正規表示式仍然會匹配 2003-02-31,而這並非有效的日期。讓前導零為可選的可以是另一個增強功能。

如果您想要強制分隔符一致,可以使用 反向參照。 ^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$ 會匹配 1999-01-01,但不會匹配 1999/01-01。

同樣地,您要讓正規表示式多麼複雜取決於您使用它的資料,以及如果出現不想要的匹配,問題會有多嚴重。如果您在腳本中驗證使用者的日期輸入,在正規表示式之外執行某些檢查可能比較容易。例如,當年份不是閏年時排除 2 月 29 日,在腳本語言中執行會容易得多。使用簡單的算術來檢查年份是否可被 4 整除(且不可被 100 整除,除非可被 400 整除),會比使用正規表示式容易得多。

以下是如何在 Perl 中檢查有效的日期。我也加入了括號,將年份擷取到反向參照中。

sub isvaliddate {
  my $input = shift;
  if ($input =~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$!) {
    # At this point, $1 holds the year, $2 the month and $3 the day of the date entered
    if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) {
      return 0; # 31st of a month with 30 days
    } elsif ($3 >= 30 and $2 == 2) {
      return 0; # February 30th or 31st
    } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 != 0 or $1 % 400 == 0))) {
      return 0; # February 29th outside a leap year
    } else {
      return 1; # Valid date
    }
  } else {
    return 0; # Not a date
  }
}

若要比對 mm/dd/yyyy 格式的日期,請將正規表示法重新排列為 ^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$。對於 dd-mm-yyyy 格式,請使用 ^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$。

Regular Expression Matching a Valid Date
  • 简
  • 繁
  • En
About Regular Expressions » Sample Regular Expressions » Regular Expression Matching a Valid Date

Examples
Regular Expressions Examples
Numeric Ranges
Floating Point Numbers
Email Addresses
IP Addresses
Valid Dates
Numeric Dates to Text
Credit Card Numbers
Matching Complete Lines
Deleting Duplicate Lines
Programming
Two Near Words
Pitfalls
Catastrophic Backtracking
Too Many Repetitions
Denial of Service
Making Everything Optional
Repeated Capturing Group
Mixing Unicode & 8-bit
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Regular Expression Matching a Valid Date

^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$ matches a date in yyyy-mm-dd format from 1900-01-01 through 2099-12-31, with a choice of four separators. The anchors make sure the entire variable is a date, and not a piece of text containing a date. The year is matched by (19|20)\d\d. I used alternation to allow the first two digits to be 19 or 20. The parentheses are mandatory. Had I omitted them, the regex engine would go looking for 19 or the remainder of the regular expression, which matches a date between 2000-01-01 and 2099-12-31. Parentheses are the only way to stop the vertical bar from splitting up the entire regular expression into two options.

The month is matched by 0[1-9]|1[012], again enclosed by parentheses to keep the two options together. By using character classes, the first option matches a number between 01 and 09, and the second matches 10, 11 or 12.

The last part of the regex consists of three options. The first matches the numbers 01 through 09, the second 10 through 29, and the third matches 30 or 31.

Smart use of alternation allows us to exclude invalid dates such as 2000-00-00 that could not have been excluded without using alternation. To be really perfectionist, you would have to split up the month into various options to take into account the length of the month. The above regex still matches 2003-02-31, which is not a valid date. Making leading zeros optional could be another enhancement.

If you want to require the delimiters to be consistent, you could use a backreference. ^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$ will match 1999-01-01 but not 1999/01-01.

Again, how complex you want to make your regular expression depends on the data you are using it on, and how big a problem it is if an unwanted match slips through. If you are validating the user’s input of a date in a script, it is probably easier to do certain checks outside of the regex. For example, excluding February 29th when the year is not a leap year is far easier to do in a scripting language. It is far easier to check if a year is divisible by 4 (and not divisible by 100 unless divisible by 400) using simple arithmetic than using regular expressions.

Here is how you could check a valid date in Perl. I also added parentheses to capture the year into a backreference.

sub isvaliddate {
  my $input = shift;
  if ($input =~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$!) {
    # At this point, $1 holds the year, $2 the month and $3 the day of the date entered
    if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) {
      return 0; # 31st of a month with 30 days
    } elsif ($3 >= 30 and $2 == 2) {
      return 0; # February 30th or 31st
    } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 != 0 or $1 % 400 == 0))) {
      return 0; # February 29th outside a leap year
    } else {
      return 1; # Valid date
    }
  } else {
    return 0; # Not a date
  }
}

To match a date in mm/dd/yyyy format, rearrange the regular expression to ^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$. For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$.

©2015-2025 艾丽卡 support@alaica.com