Products
GG网络技术分享 2025-03-18 16:15 1
I have a regex which validate birthday:
if(!preg_match(\'/^(0?[1-9]|1[012])[- .\\/](0?[1-9]|[12][0-9]|3[01])[- .\\/](19|20)?[0-9]{2}$/\', $_POST[\'bday\']){
echo \'enter your birthdate in a valid format. mm/dd/yyy\';
}
which is:
correct month
leap year date is not accurate
february accepts day 30 & 31
and the year 20 accepts greater than 2013
I only done correct with months how can I do more accurate birthday validation? or there is any alternate way to validate birthday?
my desired output is:
validate leap year date on month of february
year must not greater than 2010
OUTPUT:
02/14/2010
I\'m just quite new on regex kindly help?
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我有一个验证生日的正则表达式:</ p>
if( !preg_match(\'/ ^(0?[1-9] | 1 [012])[ - 。\\ /](0?[1-9] | [12] [0-9] | 3 [01])[ - 。\\ /](19 | 20)?[0-9] {2} $ /\',$ _POST [\'bday\']){
echo\'以有效格式输入您的出生日期.mm / dd / yyy\';
}
</ code> </ pre>
这是:</ p>
正确的月份</ strong> </ p>
闰年日期不准确</ em> </ p>
2月接受第30天和第31天</ em> </ p> \\ n
并且第20年接受超过2013年</ em> </ p>
我只用了几个月就更正确如何才能更准确地进行生日验证?或者有更正确的生日验证? 验证生日的任何其他方式?</ p>
我想要的输出是:</ p>
验证2月份的闰年日期</ strong> < / p>
年份不得大于2010年</ strong> </ p>
输出:</ p>
02/14/2010 </ code> </ p>
我对正则表达式的帮助很新吗?</ p>
</ div>
网友观点:
A regex isn\'t well-suited for parsing dates. PHP already has built-in functions and classes to do this and you should use that instead. Here\'s how this can be done using PHP\'s DateTime class (function by Glavić, from php.net):
function validateDate($date, $format = \'m/d/Y\'){
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date && $d->format(\'Y\') <= 2010;
}
For example:
var_dump(validateDate(\'02/14/2010\')); // bool(true)var_dump(validateDate(\'02/14/2011\')); // bool(false)
请问判断验证一个字符串是正则表达式的正则表达式要怎么写?
高赞 @陈壳子 的回答很难说是正确的(而且还不注明引用来源。。),然而这位关了评论只能另开回答,理由如下:
- 除非我们对”正则表达式“这个概念没有达成共识,提问者的问题应该等价于“一个由合法正则表达式组成的语言是否是正则的,如果是,给出实现”,然而答案是否定的,因为判断正则表达式是否合法时要解决的一个更小的问题是判断括号是否匹配(更正式的说,括号匹配问题可以规约到合法正则表达式的判定问题),然而括号匹配不是正则的,可以用pumping lemma,取 s=0^p1^p 来证明。
- 事实上原Stackoverflow回答下面紧接着一句:
This is a recursive regex, and is not supported by many regex engines.
从工程上来讲,这个方法“is not supported by many regex engines”,应该不太实用;从形式语言的角度来讲问题比较大,因为recursive "regular" expression is not regular.
那位引用的Stackoverflow回答 - Is there a regular expression to detect a valid regular expression?
也是Stackoverflow - Why is recursive regex not regex?
Demand feedback