VB.net从字符串获取日期

时间:2018-03-26 13:12:33

标签: vba excel-vba excel

你能帮我从字符串/文字中提取日期

假设我有以下字符串

  1. "This is a date, January 23, 2016 Stackoverflow!"
  2. "this 27 Aug 16 is in date format."
  3. "03.22.2017 is also a date."
  4. 我需要的输出就是这个。

    1. January 23, 2016
    2. 27 Aug 16
    3. 03.22.2017
    4. 基本上我在文中只需要的是日期。 请帮忙。感谢

1 个答案:

答案 0 :(得分:1)

评论中共享的@PᴇʜRegex模式完全符合您的示例。以下是如何将其与单元格函数一起使用。

请勿忘记添加对“ Microsoft VBScript正则表达式5.5 ”的引用

The requested resource does not support http method 'PUT'.

输入位于A列,B列使用公式Function simpleCellRegex(Myrange As Range) As String Dim regEx As New RegExp Dim strPattern As String Dim strInput As String Dim strOutput As String Dim matches As Object strPattern = "(?:January|Februrary|March|April|May|June|July|August|September|October|November|December) [0-9]{1,2}, [0-9]{4}|[0-9]{1,2} (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{2,4}|[0-9]{1,2}.[0-9]{1,2}.[0-9]{2,4}" If strPattern <> "" Then strInput = Myrange.Value With regEx .Global = True .MultiLine = True .IgnoreCase = False .Pattern = strPattern End With If regEx.Test(strInput) Then Set matches = regEx.Execute(strInput) If matches.Count <> 0 Then simpleCellRegex = matches.Item(0) Else simpleCellRegex = "Not matched" End If End If End Function

<强>测试

enter image description here