gpt4 book ai didi

python - python中模式匹配时间格式的正则表达式

转载 作者:行者123 更新时间:2023-12-04 09:35:58 26 4
gpt4 key购买 nike

我希望在 python 中使用正则表达式匹配以下时间格式,并在找到/未在一行中找到匹配时标记 True 或 False。示例文本如下。如何仅使用正则表达式来完成此任务?

  • 凌晨 2 点至晚上 8 点
  • 上午 2:00 - 晚上 8:00
  • 08:00am-05:00pm
  • 上午 5:30 - 晚上 8:59

  • 人们可以观察到'_am - _pm' 和'_am-_pm' 模式在每个符号中都是一致的。冒号和空格匹配的数字格式是我一直在尝试做的。以下是我从 here 中发现的内容
    HH:MM 12-hour format, optional leading 0, mandatory meridiems (AM/PM)
    /((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))/
    示例文本:
    Lorem Ipsum is dummy text of the printing and typesetting industry between 2am-8pm. 
    Contrary to popular belief, Lorem Ipsum is not simply random text. : False
    Lorem has been the industry between 2:00am - 8:00pm standard dummy text since the 1500s.
    It has survived not only five centuries, but also between 08:00am-05:00pm
    It was popularised from 5:30am - 8:59pm with the release of Letraset sheets.
    More recently with desktop publishing software like Aldus PageMaker 983-765-0976.
    期望的输出:
    Lorem Ipsum is dummy text of the printing and typesetting industry between 2am-8pm. : True
    Contrary to popular belief, Lorem Ipsum is not simply random text. : False
    Lorem has been the industry between 2:00am - 8:00pm standard dummy text since the 1500s. : True
    It has survived not only five centuries, but also between 08:00am-05:00pm : True
    It was popularised from 5:30am - 8:59pm with the release of Letraset sheets. : True
    More recently with desktop publishing software like Aldus PageMaker 983-765-0976. : False

    最佳答案

    您可以使用

    (?i)(?<!\d)(?:1[0-2]|0?[1-9])(?::(?:[0-5][0-9]))?\s?[ap]m\s*-\s*(?:1[0-2]|0?[1-9])(?::(?:[0-5][0-9]))?\s?[ap]m\b
    regex demo
    详情
  • (?i) -
  • 上的不区分大小写模式
  • (?<!\d) - 不允许前面有数字
  • (?:1[0-2]|0?[1-9])(?::(?:[0-5][0-9]))? - 时间模式:
  • (?:1[0-2]|0?[1-9]) - 012带有可选的前导 0之前 1-9数字
  • (?::(?:[0-5][0-9]))? - 一个可选的分钟序列,带有 :分隔符

  • \s? - 一个可选的空格
  • [ap]m - ap然后 m
  • \s*-\s* - 用 0+ 个空格括起来的连字符
  • (?:1[0-2]|0?[1-9])(?::(?:[0-5][0-9]))?\s?[ap]m - 与上述相同的时间模式
  • \b - 字边界。

  • Python demo :
    import re
    time = r'(?:1[0-2]|0?[1-9])(?::(?:[0-5][0-9]))?\s?[ap]m'
    pattern = re.compile(r'(?i)(?<!\d){0}\s*-\s*{0}\b'.format(time))
    texts = ['Lorem Ipsum is dummy text of the printing and typesetting industry between 2am-8pm.',
    'Contrary to popular belief, Lorem Ipsum is not simply random text.',
    'Lorem has been the industry between 2:00am - 8:00pm standard dummy text since the 1500s.',
    'It has survived not only five centuries, but also between 08:00am-05:00pm',
    'It was popularised from 5:30am - 8:59pm with the release of Letraset sheets.',
    'More recently with desktop publishing software like Aldus PageMaker 983-765-0976.']
    for text in texts:
    print (text, bool(pattern.search(text)), sep=" : ")
    输出:
    Lorem Ipsum is dummy text of the printing and typesetting industry between 2am-8pm. : True
    Contrary to popular belief, Lorem Ipsum is not simply random text. : False
    Lorem has been the industry between 2:00am - 8:00pm standard dummy text since the 1500s. : True
    It has survived not only five centuries, but also between 08:00am-05:00pm : True
    It was popularised from 5:30am - 8:59pm with the release of Letraset sheets. : True
    More recently with desktop publishing software like Aldus PageMaker 983-765-0976. : False

    关于python - python中模式匹配时间格式的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62584721/

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