加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式特定的重复间隔解析器,大多数工作

发布时间:2020-12-14 05:58:47 所属栏目:百科 来源:网络整理
导读:我有以下正则表达式: once|every (?:(?:([1-9][0-9]?[0-9]? )?(hours?|days?))|(?:(?:(?:(1st|2nd|3rd|4th|5th))? ?(monday|tuesday|wednesday|thursday|friday|saturday|sunday)?)|((?:[1-9]|[1-2][0-9]|3[0-1])(?:st|nd|rd|th))) ?(?:(of the month)? ?(?
我有以下正则表达式:

once|every (?:(?:([1-9][0-9]?[0-9]? )?(hours?|days?))|(?:(?:(?:(1st|2nd|3rd|4th|5th))? ?(monday|tuesday|wednesday|thursday|friday|saturday|sunday)?)|((?:[1-9]|[1-2][0-9]|3[0-1])(?:st|nd|rd|th))) ?(?:(of the month)? ?(?:(?:repeated every )([2-9][0-9]?[0-9]?) (weeks|months))?)?)

这适用于我需要的大部分内容,但不适用于以下字符串…

Every 20th of the month

以下工作很棒……

Once
Every hour
Every day
Every 2 hours
Every 2 days
Every Monday
Every 1st of the month
Every 1st of the month repeated every 3 months
Every 2nd Monday of the month
Every 2nd Monday of the month repeated every 3 months

你能帮我吗?
谢谢

解决方法

问题是(?:(?:( 1st | 2nd | 3rd | 4th | 5th))?(星期一|星期二|星期三|星期四|星期五|星期六|星期日)?)部分:它可能匹配一个空字符串.因此,它总是“赢”.

您可以通过删除来修复它吗?星期日之后的量词,使星期几成为强制性模式.见this regex demo.

另外,如果你用正则表达式检查的字符串是独立的,你可以用^(?:和)$模式将整个模式括起来,使它们匹配整个模式.见this regex demo.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读