gpt4 book ai didi

c# - 如何在随机字符串中获取日期时间?

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:08 25 4
gpt4 key购买 nike

我得到了一堆告诉我有关事件的字符串,我需要将所有字符串转换为开始 DateTime 和结束 DateTime。起初,我尝试逐个字符地遍历字符串,但当字符串中有多个日期时,它变得太复杂了。我也尝试过解析许多日期格式,但是当日期和月份先出现,然后是时间时,它就不起作用了。我正在使用 C#,也曾尝试使用 Regex 搜索字符串,但我遇到了麻烦,因为我无法将日期与正确的时间匹配。

这是我给出的字符串的几个示例:

September 12-13,2015, Saturday 10:30 a.m.-6p.m. Sunday 10 a.m. noon

应该有 2 个日期:

StartDate: 2015/09/12 10:30 EndDate: 2015/09/12 18:00
StartDate: 2015/09/13 10:00 EndDate: 2015/09/13 12:00

June 3 - September 9, 2015, Tuesday-Thursday 6-7 p.m. , Sunday 10-11 a.m

星期二/星期四/星期日的多个日期以及日期范围:

StartDate: 2015/06/04 18:00 EndDate: 2015/06/04 19:00
StartDate: 2015/06/07 10:00 EndDate: 2015/06/07 11:00
StartDate: 2015/06/09 18:00 EndDate: 2015/06/09 19:00
StartDate: 2015/06/11 18:00 EndDate: 2015/06/11 19:00

...继续遵循相同的模式

谢谢。

最佳答案

这是一种可能的方法:

1) 扫描/Lexing -> 扫描基本 token 。

Names: September, Saturday, AM, etc.
Numbers: 12, 2015, 9, etc.
Operators serving as Separators: '-', ',', space, etc.
'-' acts as a range operator as in FromDate - ToDate.
',' and space separate components of a date

2) 解析 -> 用标记构建一个解析树。

Names are classified into Months, days of week, etc.
Numbers are identified as year, month or day if it
can be done unambiguously. Otherwise, their identification
is left to later steps.
We can use some heuristics, like day of month almost always follows month.

3) 现在,解析树表示由“-”分隔的日期时间条目。

At this point, a date in the tree can be partial or complete.
Introduce separator when it is missing between adjacent dates or times.
"Sunday 10a.m noon" is missing separator between '10am' and 'noon'

4) 从解析树中识别完整和部分日期。

For example, "September 9, 2015" is a complete date, while "June 3"
is incomplete. After extracting at least one complete date, infer
the missing elements in incomplete dates from surrounding context.
"June 3" is incomplete because of missing year, so we grab the
year from the nearest complete date as 2015.

5) 如果在上述步骤中找不到完整的日期,

Use two adjacent dates and let them fill in missing parts
from each other to arrive at a complete one. "September 12 - 13, 2015"
is one such example. Left side of the separator is missing
year and can get it from right side. Figure out the date for
a day of week, like Thursday from the complete date in the string

关于c# - 如何在随机字符串中获取日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32929799/

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