我有非结构化的字符串。从这里我需要找到日期。
示例:预期投入
here"
G 211
注:日期可采用任何格式,如2012年1月1日、2012年1月12日至2012年1月、2012年1月12日至1月1日、2012年1月1日至1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月1日、2012年1月1日至2012年1月
任何帮助都是徒弟的。
发布于 2012-02-08 13:18:55
为什么用户输入允许这样的自由形式的文本开始?对于开放的任何字符串解析的输入,您所做的最多只能是一塌糊涂。如果用户输入的数字看起来像日期或另一个日期呢?您将如何确定哪个日期是您需要跟踪的“日期”?
关于您的问题的更多信息可能有助于解决方案,但现在我建议将日期输入到它自己的输入元素中。
发布于 2012-02-08 21:07:57
将字符串解析为由空格替换的连续块,看起来像string.split(“")几乎可以工作,但是您可能需要对”:“进行解释。
在每个街区上,使用DateTime.TryParse进行检查。
Dim text(2) As String
text(0) = "01/21/2012: text will be here"
text(1) = "text will be here. \n 01/21/2012: continues text"
text(2) = " text will be here 01/21/2012"
For Each s As String In text
Dim a As String() = s.split(" "c)
For Each s1 As String In a
If s1.endswith(":") Then s1 = s1.remove(s1.length-1)
Dim dt As datetime
Dim ok As Boolean = datetime.tryparse(s1,dt)
If ok = True Then output.writeline(dt.tostring)
Next s1
Next s发布于 2012-02-08 13:19:03
最好的方法是使用RegEx,但是您必须为所有日期格式创建规则。否则,您可以使用更通用的regex表达式,然后查找所有匹配并验证/解析为日期。
希望这能让你知道该怎么做。
https://stackoverflow.com/questions/9194011
复制相似问题