热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

有规律的表达-Regularexpressionfortimeofaday

Iamtryingtocreatearegularexpressionfortimeofaparticularday.Ithandles7amand11:34pm

I am trying to create a regular expression for time of a particular day. It handles 7am and 11:34pm correctly. It should be able to identify the ill-formed times. Now I have tried writing a working regular expression for the problem.

我正在尝试为某一天的某个时间创建一个正则表达式。正确处理早上7点和晚上11点34分。它应该能够识别不良的时代。现在我试着为这个问题写一个正常的表达式。

Regular Expression^(([0]?[0-9])|([1][0-2]))(\:[00-59]{2})?(am|pm){1}\b

正则表达式^(([0 - 9][0]?)|([1][0]))(\[00-59]{ 2 })?(我| pm){ 1 } \ b

The problem is that in antemeridiem, the hour part varies from 1(or 01) to 12 but in postmeridiem it varies from 0(or 00) to 12. I am not able to deduce the regex for this situation. Kindly help.

问题是,在前梅里蒂姆,小时部分从1(或01)到12,但在后梅里蒂姆,小时部分从0(或00)到12。我无法为这种情况推断regex。请帮助。

5 个解决方案

#1


2  

I'm not going to start arguing what the 12-hour-system is supposed to be like, but just use your requirements as given/fixed:

我不会开始争论12小时制应该是什么样的,但只要按照给定/固定的要求来使用:

The problem is that in antemeridiem, the hour part varies from 1(or 01) to 12 but in postmeridiem it varies from 0(or 00) to 12. I am not able to deduce the regex for this situation. Kindly help.

问题是,在前梅里蒂姆,小时部分从1(或01)到12,但在后梅里蒂姆,小时部分从0(或00)到12。我无法为这种情况推断regex。请帮助。

Then, this regexp should work:

然后,这个regexp应该可以工作:

^(((0?[1-9]|1[012])(:[0-5][0-9])?am)|((0?[0-9]|1[012])(:[0-5][0-9])?pm))\b

See for yourself here.

你自己看。

#2


1  

You could always do something like:

你可以这样做:

(my regex for am) | (my regex for pm)

(my regex for am) | (my regex for pm)

As long as you have your parenthesis in the right place it should work fine.

只要你把括号放在正确的位置,它就会正常工作。

#3


0  

Try something like this:

试试这样:

^(0?[1-9]|1[0-2])(:([0-5]?[0-9]))?(am|pm)$

^(0 ?(1 - 9)| 1[0])(:((0 - 5)吗?[0 - 9]))?(点|点)美元

#4


0  

This should match your requirements.

这应该符合您的需求。

^(([0]?[0-9])|([1][0-2])){1}(\:)?([0-5][0-9])?(am|pm){1}

^(([0 - 9][0]?)|([1][0])){ 1 }(\:)?([0 - 9][0 - 5])?(我| pm){ 1 }

Also, you're trying to use regex to match numbers

另外,您正在尝试使用regex来匹配数字

[00-59]

I don't think it works that way, you've to match strings, it would be better done as [0-5][0-9] instead.

我不认为它是这样工作的,你必须匹配字符串,最好是[0-5][0-9]。

#5


0  

I am trying to find a good regex for this as well. I used a tool called Expresso and came up with the following expression for 12hr time matching:

我也在寻找一个好的regex。我使用了一个叫做Expresso的工具,想出了下面12小时时间匹配的表达式:

((1{0,1}[012]{1})|[0-9]{1}){1}((:|\s)?[0-5]{1}[0-9]{1})?((a|p|A|P){1}(m|M){1})?

I tested against the following data:

我测试了以下数据:

12:00
12am
11111111PM
pm
11122am
13
00am
9a

and the expression came up with the following matches:

这个短语的意思是:

12:00
12am
1111
1111PM
1112
2am
1
3
0
0am
9

If you are validating data and compare matches to data length you can throw out the partial matches found from 11111111PM and 11122am. This works for me.

如果您正在验证数据并将匹配项与数据长度进行比较,您可以将11111111111111 pm和11122am中找到的部分匹配项排除在外。这适合我。


推荐阅读
author-avatar
木_妍_595
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有