如何为5个日期/时间的5个方案编写正则表达式?

发布于 2025-02-10 02:48:37 字数 868 浏览 2 评论 0原文

我正在寻找一种解决方案的正则表达式,该解决方案将接受/接受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})/')

这不是用于日期验证。此信息已经更新,仅选择。我需要将值分开,以便将一个方法存储在记录中,并在另一个记录上以不同的方式存储。目的是解析记录并将其变成对象。这个问题已经回答。

https://onlinephp.io演示

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.

https://onlinephp.io demo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贪了杯 2025-02-17 02:48:37
  • 这不是用于日期验证。
  • 正则表达式将接受/接受5个日期/时间变体

方式简单地使得正则更简单。

  • ,因此要捕获和提取日期和时间字符串,您可以通过以下 代码>时间:,<代码>日期/时间:使用 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。现在,我们使用

    对两组进行可选匹配。

摘要:

<?php

$tests = [
        '01/01/2022',
        '12:12:00',
        'Time: 12:12:00',
        'Date: 01/01/2022',
        'Date/Time: 01/01/2022 12:12:00'
    ];
    
foreach($tests as $test){
    preg_match('/(Date\/Time|Time|Date)\:/', $test, $possible_metadata);
    $metadata = $possible_metadata[1] ?? "";
    var_dump($metadata);
    $test = trim(str_ireplace(['Date/Time:','Time:','Date:'],'', $test));
    preg_match('/^(\d\d\/\d\d\/\d{4})?\s*(\d\d:\d\d:\d\d)?$/', $test, $matches);
    print_r($matches);
}

在线demo

  • This is not for date validation.
  • regular expression that would take/accept 5 variations of Date/Time

Ok, so to capture and extract date and time strings, you can make your regex much simpler by:

  • Stripping of Date:, Time:, Date/Time: using str_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 as HH:ii:ss as \d\d:\d\d:\d\d. Now, we perform optional matches for both groups using ?.

Snippet:

<?php

$tests = [
        '01/01/2022',
        '12:12:00',
        'Time: 12:12:00',
        'Date: 01/01/2022',
        'Date/Time: 01/01/2022 12:12:00'
    ];
    
foreach($tests as $test){
    preg_match('/(Date\/Time|Time|Date)\:/', $test, $possible_metadata);
    $metadata = $possible_metadata[1] ?? "";
    var_dump($metadata);
    $test = trim(str_ireplace(['Date/Time:','Time:','Date:'],'', $test));
    preg_match('/^(\d\d\/\d\d\/\d{4})?\s*(\d\d:\d\d:\d\d)?$/', $test, $matches);
    print_r($matches);
}

Online Demo

感情废物 2025-02-17 02:48:37

如果您想使其有点模块化以稍后扩展,则可以指定独立子例程使用(?(定义)并制作日期的模式,并且时间更具体。

请注意,这仍然不会验证日期本身。

然后,您可以组装模式使用日期的可选非捕获组:等,结果中的数组键将显示哪个变体作为交替

|测试3个变化, 结果并保留具有值的命名组。

过滤 代码>要启用当前的REGEX符号并忽略Whitespaces,并且J启用重复的子模式名称,例如dt

$strings = [
    '01/01/2022',
    '12:12:00',
    'Time: 12:12:00',
    'Date: 01/01/2022',
    'Date/Time: 01/01/2022 12:12:00',
    'test 01/01/2024'
];

$re = '~(?(DEFINE)
  (?<d>\b(?:0[1-9]|1[012])/(?:0[1-9]|[12][0-9]|3[01])/(?:19|20)\d\d)\b
  (?<t>\b(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d)\b
  (?<dt>(?&d)\h+(?&t))
)
^(?:(?:Date:\h*)?(?<d>(?&d))|(?:Time:\h*)?(?<t>(?&t))|(?:Date/Time:\h*)?(?<dt>(?&dt)))$~mxJ';

foreach ($strings as $str) {
    if (preg_match($re, $str, $matches)) {
        $result = array_filter($matches, function ($v, $k) {
            return !(empty($v)) && !is_int($k);
        }, ARRAY_FILTER_USE_BOTH);
        
        print_r($result);
    }
}

输出,

Array
(
    [d] => 01/01/2022
)
Array
(
    [t] => 12:12:00
)
Array
(
    [t] => 12:12:00
)
Array
(
    [d] => 01/01/2022
)
Array
(
    [dt] => 01/01/2022 12:12:00
)

请参见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 for multiline as there are anchors, extended to enable the current regex notation and ignore whitespaces, and J to enable the duplicate sub pattern names like dt

$strings = [
    '01/01/2022',
    '12:12:00',
    'Time: 12:12:00',
    'Date: 01/01/2022',
    'Date/Time: 01/01/2022 12:12:00',
    'test 01/01/2024'
];

$re = '~(?(DEFINE)
  (?<d>\b(?:0[1-9]|1[012])/(?:0[1-9]|[12][0-9]|3[01])/(?:19|20)\d\d)\b
  (?<t>\b(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d)\b
  (?<dt>(?&d)\h+(?&t))
)
^(?:(?:Date:\h*)?(?<d>(?&d))|(?:Time:\h*)?(?<t>(?&t))|(?:Date/Time:\h*)?(?<dt>(?&dt)))$~mxJ';

foreach ($strings as $str) {
    if (preg_match($re, $str, $matches)) {
        $result = array_filter($matches, function ($v, $k) {
            return !(empty($v)) && !is_int($k);
        }, ARRAY_FILTER_USE_BOTH);
        
        print_r($result);
    }
}

Output

Array
(
    [d] => 01/01/2022
)
Array
(
    [t] => 12:12:00
)
Array
(
    [t] => 12:12:00
)
Array
(
    [d] => 01/01/2022
)
Array
(
    [dt] => 01/01/2022 12:12:00
)

See a PHP demo

枕头说它不想醒 2025-02-17 02:48:37

根据先前收集的信息,最好的路线是简单地使用:

preg_match('/(^)?((date|time|(date(?:(?!\/time))?\/time))\:)?(.*)/im');

在第五字段中,将是时间戳(日期?/time),在第3组中,它将告诉其名称(日期/时间/时间/日期时间/日期时间/日期时间)。

Based on the information previously gathered, the best route is to simply use:

preg_match('/(^)?((date|time|(date(?:(?!\/time))?\/time))\:)?(.*)/im');

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文