gpt4 book ai didi

正则表达式从字符串中提取时间(例如 7 :30pm, 8 pm, 9.05)

转载 作者:行者123 更新时间:2023-12-01 08:30:01 24 4
gpt4 key购买 nike

我正在开发一个 Rails 应用程序,它使用外部提要来获取某些事件数据,但令人讨厌的是,它们只提供一个包含时间的字符串。例如:

Doors open at 7:30pm, show starts at 9pm



我的目标是从这些字符串中提取第一次并将其放入日期时间字段。系统需要捕获以下类型的值:
  • 上午 11 点
  • 中午 12 点
  • 下午 1 点
  • 下午 2:15
  • 3.30pm
  • 4.45
  • 5:30
  • 06:15
  • 07:30pm
  • 晚上 8:30
  • 晚上 9.15

  • 但不是这些:
  • 105
  • 2 50
  • 305pm
  • 4 点 15 点
  • 74pm
  • 840am

  • 我认为最好的方法是使用正则表达式并通过一些搜索(特别是 this SO question )我有以下内容:
    [0-9]{1,2}(:|.)??[0-9]{0,2}\s?(am|pm|AM|PM)
    它部分有效,但不排除任何我不想要的,似乎只捕获 2 和 3 中 am/pm 的第一个字符。

    这可以用正则表达式吗?

    谢谢!

    最佳答案

    \b((?:0?[1-9]|1[0-2])(?!\d| (?![ap]))[:.]?(?:(?:[0-5][0-9]))?(?:\s?[ap]m)?)\b

    它不支持 24 小时格式,但它强制执行有效时间。向你的正则表达式引擎添加一个不区分大小写的标志,无论它是什么语言,或者用 (i: ) 包裹正则表达式如果支持。

    Demo with your sample

    正则表达式:
    NODE                     EXPLANATION
    --------------------------------------------------------------------------------
    \b the boundary between a word char (\w) and
    something that is not a word char
    --------------------------------------------------------------------------------
    ( group and capture to \1:
    --------------------------------------------------------------------------------
    (?: group, but do not capture:
    --------------------------------------------------------------------------------
    0? '0' (optional (matching the most
    amount possible))
    --------------------------------------------------------------------------------
    [1-9] any character of: '1' to '9'
    --------------------------------------------------------------------------------
    | OR
    --------------------------------------------------------------------------------
    1 '1'
    --------------------------------------------------------------------------------
    [0-2] any character of: '0' to '2'
    --------------------------------------------------------------------------------
    ) end of grouping
    --------------------------------------------------------------------------------
    (?! look ahead to see if there is not:
    --------------------------------------------------------------------------------
    \d digits (0-9)
    --------------------------------------------------------------------------------
    | OR
    --------------------------------------------------------------------------------
    ' '
    --------------------------------------------------------------------------------
    (?! look ahead to see if there is not:
    --------------------------------------------------------------------------------
    [ap] any character of: 'a', 'p'
    --------------------------------------------------------------------------------
    ) end of look-ahead
    --------------------------------------------------------------------------------
    ) end of look-ahead
    --------------------------------------------------------------------------------
    [:.]? any character of: ':', '.' (optional
    (matching the most amount possible))
    --------------------------------------------------------------------------------
    (?: group, but do not capture (optional
    (matching the most amount possible)):
    --------------------------------------------------------------------------------
    (?: group, but do not capture:
    --------------------------------------------------------------------------------
    [0-5] any character of: '0' to '5'
    --------------------------------------------------------------------------------
    [0-9] any character of: '0' to '9'
    --------------------------------------------------------------------------------
    ) end of grouping
    --------------------------------------------------------------------------------
    )? end of grouping
    --------------------------------------------------------------------------------
    (?: group, but do not capture (optional
    (matching the most amount possible)):
    --------------------------------------------------------------------------------
    \s? whitespace (\n, \r, \t, \f, and " ")
    (optional (matching the most amount
    possible))
    --------------------------------------------------------------------------------
    [ap] any character of: 'a', 'p'
    --------------------------------------------------------------------------------
    m 'm'
    --------------------------------------------------------------------------------
    )? end of grouping
    --------------------------------------------------------------------------------
    ) end of \1
    --------------------------------------------------------------------------------
    \b the boundary between a word char (\w) and
    something that is not a word char

    关于正则表达式从字符串中提取时间(例如 7 :30pm, 8 pm, 9.05),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20958224/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com