如何为5个日期/时间的5个方案编写正则表达式?
我正在寻找一种解决方案的正则表达式,该解决方案将接受/接受5个日期/时间
- 01/01/2022
- 12:12:00
- 时间:12:12:
- 01/01/2022 DATE
- 00日期: /TIME:01/ 01/2022 12:12:00
到目前为止,我所有的工作就是以下内容:但是日期:...和01/01/2022没有触发。到目前为止,这是我可以在互联网上提出的资源。
preg_match('/(^)?((date|time|(date(?:(?!\/time))?\/time))\:.)?(\d{1,2}\/\d{1,2}\/\d{4})?(.)?(\d{1,2}\:\d{1,2}\:\d{1,2})/')
这不是用于日期验证。此信息已经更新,仅选择。我需要将值分开,以便将一个方法存储在记录中,并在另一个记录上以不同的方式存储。目的是解析记录并将其变成对象。这个问题已经回答。
I am looking for a solution to regular expression that would take/accept 5 variations of Date/Time
- 01/01/2022
- 12:12:00
- Time: 12:12:00
- Date: 01/01/2022
- Date/Time: 01/01/2022 12:12:00
So far all I have working is the following: But the Date: ... and 01/01/2022 are no triggering. So far this is the best I could come up with the resources on the internet.
preg_match('/(^)?((date|time|(date(?:(?!\/time))?\/time))\:.)?(\d{1,2}\/\d{1,2}\/\d{4})?(.)?(\d{1,2}\:\d{1,2}\:\d{1,2})/')
This is not for date validation. This information has already been updated, and is only being selected. I needed to separate the values so incase one was stored 1 way on the record, and differently on another record. The purpose is to parse a record and turning it into an object. This question has been answered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方式简单地使得正则更简单。
str_ireplace
。现在,我们只剩下实际的日期和时间值以匹配。创建一个正则匹配以匹配
dd/mm/yyyy 的日期格式,为
\ d \ d \ d/\ d/\ d/\ d/\ d/\ d/\ d {4}
,类似地时间为hh:ii:ss
作为\ d \ d:\ d \ d:\ d \ d \ d
。现在,我们使用?
。对两组进行可选匹配。
摘要:
在线demo
Ok, so to capture and extract date and time strings, you can make your regex much simpler by:
Stripping of
Date:
,Time:
,Date/Time:
usingstr_ireplace
. Now, we are only left with actual date and time values to match.Creating a regex to match a date format of
dd/mm/YYYY
as\d\d/\d\d/\d{4}
and similarly for time asHH:ii:ss
as\d\d:\d\d:\d\d
. Now, we perform optional matches for both groups using?
.Snippet:
Online Demo
如果您想使其有点模块化以稍后扩展,则可以指定独立子例程使用
(?(定义)
并制作日期的模式,并且时间更具体。请注意,这仍然不会验证日期本身。
然后,您可以组装模式使用
日期的可选非捕获组:
等,结果中的数组键将显示哪个变体作为交替|
测试3个变化, 结果并保留具有值的命名组。过滤 代码>要启用当前的REGEX符号并忽略Whitespaces,并且
J
启用重复的子模式名称,例如dt
输出,
请参见a php demo
If you want to make it a bit modular to perhaps extend it later on, you can specify separate subroutines using
(?(DEFINE)
and make the patterns for the date and the time a bit more specific.Note that this will still not validate the date itself.
Then you can assemble the pattern with optional non capture groups for
Date:
etc, and the array key in the result will show for which variant there is a match.As the alternation
|
tests for 3 variations, filter the result and keep the named group that has a value.The
mxJ
flags at the end of the pattern stand formultiline
as there are anchors,extended
to enable the current regex notation and ignore whitespaces, andJ
to enable the duplicate sub pattern names likedt
Output
See a PHP demo
根据先前收集的信息,最好的路线是简单地使用:
在第五字段中,将是时间戳(日期?/time),在第3组中,它将告诉其名称(日期/时间/时间/日期时间/日期时间/日期时间)。
Based on the information previously gathered, the best route is to simply use:
That way in the 5th field will be the timestamp (date?/time), and in the group 3 it will tell its name (Date/Time/Date-Time).