是否有更有效的方法来生成日期的REGEX字符串

Is there a more efficient way to generate a REGEX String for Dates

本文关键字:日期 REGEX 字符串 方法 有效 是否      更新时间:2023-10-16

我正在为QT项目寻找一个更高效的正则表达式。

以下正则表达式匹配的日期字符串:

8 June 2009
2009-06-08
1/31/1971
ca. 1971
circa 1971
about 1971
abt 1971
before 1971
bef 1971
bante 1971
after 1971
aft 1971
apost 1971
between 1970 and 2000
bet 1970 and 2000
btw 1970 and 2000
1970-2000
from 1970 to 2000
est 2000
2000 or 2001
2000 | 2001
2019
1.10.2019
June 2009
6/2019

表达式:

^((?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)[t ]+[0-9]{4})|^((?:3[01]|[12][0-9]|0?[1-9])[t ]+(?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)[t ]+[0-9]{4})|^([0-9]{4} or [0-9]{4})|(^(?:1[0-2]|0?[1-9])[/.-][0-9]{4})|(^[0-9]{4}-[0-9]{4})|(^from[t ]+[0-9]{4}[t ]+to[t ]+[0-9]{4})|(^btw[t ]+[0-9]{4}[t ]+and[t ]+[0-9]{4})|(^between[t ]+[0-9]{4}[t ]+and[t ]+[0-9]{4})|(^[tnvfr ]*([0-9]{4})$)|(^[0-9]{4}[t ]+|[t ]+[0-9]{4}b)|(^(?:1[0-2]|0?[1-9])[/.-](?:3[01]|[12][0-9]|0?[1-9])[/.-][0-9]{4})|(^est[t ]+[0-9]{4})|(^after[t ]+[0-9]{4})|(^before[t ]+[0-9]{4})|(^bef[t ]+[0-9]{4})|(^abt[t ]+[0-9]{4})|(^about[t ]+[0-9]{4})|(^circa[t ]+[0-9]{4})|(^ca[^nr][t ]+[0-9]{4})|(^bante[t ]+[0-9]{4})|(^aft[t ]+[0-9]{4})|(^apost[t ]+[0-9]{4})|(^btw[t ]+[0-9]{4}[t ]+and[t ]+[0-9]{4})|(^bet[t ]+[0-9]{4}[t ]+and[t ]+[0-9]{4})|(^[0-9]{4}-(?:1[0-2]|0?[1-9])-(?:3[01]|[12][0-9]|0?[1-9]))

验证结果匹配https://rubular.com/r/boBqo3h0KBt7oARubularRebex

我能找到的唯一解决方案是使用交替组执行。这是匹配字符串日期列表的合理方法吗?

提前感谢!

我将提交RegEx作为发布和目标集中的RegEx,作为必要的额外检查。谢谢你的评论。