perl 正则表达式中的#_在Perl中使用正则表达式进行日期验证

perl 正则表达式中的#

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration (minimum).

不管是什么原因,如果您从事Web开发方面的工作,则需要在任何编辑页面上或在注册时说(例如最小)的日常验证代码,例如电子邮件验证,日期验证,IP地址验证,电话验证。 )。

You can do these by using JavaScript (client-side) or through any language like PHP, Perl etc. (Back end side). As right now I am working on Perl, so my validation codes would be in Perl only.  

您可以使用JavaScript(客户端)或通过任何语言(如PHP,Perl等)(后端)来完成这些操作。 目前,我正在使用Perl,因此我的验证代码仅在Perl中。

Date Validation using Regular expression in Perl

在Perl中使用正则表达式进行日期验证

Date consists of three main things i.e. Date of the month, month of the year and year itself and one more thing that is required is the separator (/ or –  ). You may choose any other separator, no issues. So, after combining all these things we will get date like MM/DD/YYYY or DD/MM/YYYY or YYYY/MM/DD (you can use – or anything instead of /, that I used here). DD being the date, MM being the month and YYYY being the year.

日期包含三项主要内容,即月份的日期,一年中的月份和年份本身,而分隔符(/或–)则是另一项要求。 您可以选择任何其他分隔符,没有问题。 因此,将所有这些内容组合在一起后,我们将获得日期,例如MM / DD / YYYY或DD / MM / YYYY或YYYY / MM / DD(您可以使用–或任何代替/的东西,我在这里使用)。 DD是日期,MM是月,而YYYY是年。

So , to  accomplish the task, we have to check that:

因此,要完成任务,我们必须检查一下:

a.     DD of the date should be between 1 and 31 ,

一个。 日期的DD应该在1到31之间,

b.    MM should be between 1 and 12

b。 MM应该在1到12之间

c.   YYYY should be from 1900 and till date (Instead of today’s date you can put  boundary of any date)

C。 YYYY应该是从1900年到现在的日期(您可以输入今天的边界,而不是今天的日期)

d.   If MM is 02 i.e. February, then DD should be between 1 and 28 and if its leap year then DD can extend up to 29.

d。 如果MM是02,即2月,则DD应该在1到28之间,如果其leap年,则DD可以扩展到29。

So, here is the magic of line which will do all validation itself, using regular expression:

因此,这是line的神奇之处,它将使用正则表达式自行完成所有验证:

It would match dd-mm-yyyy or dd/mm/yyyy pattern for rest of the patterns you have to change the code little bit.

对于其余的模式,您将需要稍微更改代码,所以它将与dd-mm-yyyy或dd / mm / yyyy模式匹配。

$pattern='^(0[1-9]|[12][0-9]|3[01])[- /](0[1-9]|1[012])[- /]((19|20)\d\d)$';

If date is  single digit only, put 0 in-front of it, then only it will work.

如果日期仅是一位数字,则将其放在0前面,然后它才有效。

Explanation

说明

First parenthesis is for date which will allow you to put only 01-31 then second parenthesis is for month which will allow you to enter from 01-12 only.

第一个括号用于日期,您只能输入01-31,第二个括号用于日期,月份只允许您输入01-12。

Last parenthesis is for Year which will take values from 1900 to 2099 (at max but you can restrict it to today’s date by using localtime())

最后一个括号是Year,它将取值1900到2099(最大值,但是您可以使用localtime()将其限制为今天的日期)

#!C:/strawberry/perl/bin/perl.exe     # path to my perl compiler, change it with yours ex: #!/usr/bin/perl
#########################################
# Author: Sanjeev Kumar Jaiswal
# Date: October 2010
# Purpose: Date Validation in any format you wish.
# ex: dd-mm-yyyy or dd/mm/yyyy
########################################
use strict;
use warnings;
my @today = localtime();  #get current system time
my $year = $today[5]+1900;
my $month = $today[4]+1;
my $date = $today[3];
print "Todays is ", scalar localtime, "\n";
print "This program is to check the validation of date\n From 1900 to till now i.e.", $today[5]+1900 ," only\n";
print "Please enter the date in specified format i.e. DD/MM/YYYY or DD-MM-YYYY\n";
my $mydate=<STDIN>;   # to get the input from keyboard
chomp($mydate);    # have to remove \n from the last that came after hitting enter
# Pattern for date validation
#dd-mm-yyyy  from 1900-2099 (Year)
my $pattern='(0[1-9]|[12][0-9]|3[01])[- /](0[1-9]|1[012])[- /]((19|20)\d\d)';  
# If it matches the pattern then
if($mydate=~ m/$pattern/){
     if($2==02 && $1>29){
     print "February can't be of more than 28 or 29 days";
     exit;
     }
     if(!($3 % 4 == 0 && ($3 % 100 != 0 || $3 % 400 == 0)) && $2==2 && $1==29){
     print "Its not a leap year, so please type the date between 1 and 28";
     exit;	
     }
     if($1 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)){
     print "This month only allow upto 30 as th date input";
     exit;
     } 
     if($3>$year){
     print "The provided date is the future date i guess. its year should be less or equal to $year\n"; 
     exit;             
     }
     if($3 >= $year){
        if($2>$month){
	print "You have entered $2 at Month's place which is ahead of $month";
	exit;
	 }
	else{
	     if($1>$date){
	     print "You have entered $1 at Date's place which is ahead of $date";
	     exit;
             }
	}
    }
 print "You have entered $mydate, which is correct according to us";
}
else{
print "$mydate does not follow the pattern DD/MM/YYYY or DD-MM-YYYY  or \n you have entered some junk date like\n 1. 32 as date or \n 2. 13 as month or\n 3. 3100 as year";
}
############# End Program ################

Quote

引用

Main part was only that Regular Expression(RegEx) which solves our problem within a line. You can use that line in any language like PHP,JavaScript, ASP.NET etc.

主要部分只是正则表达式(RegEx),它可以在线解决我们的问题。 您可以使用任何语言(例如PHP,JavaScript,ASP.NET等)使用该行。

Source: Alien Coders Forum

资料来源: 外星人编码论坛

翻译自: https://www.experts-exchange.com/articles/4004/Date-validation-using-regular-expression-in-perl.html

perl 正则表达式中的#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值