【问题标题】:Splitting String in VBA using RegEx使用正则表达式在 VBA 中拆分字符串
【发布时间】:2015-03-22 07:42:13
【问题描述】:

我是 VBA 新手,想寻求一些关于使用 RegEx 的帮助,我希望能以某种方式启发我我做错了什么。我目前正在尝试将日期拆分为单独的日期、月份和年份,可能的分隔符包括“、”、“-”和“/”。

Function formattedDate(inputDate As String) As String

    Dim dateString As String
    Dim dateStringArray() As String
    Dim day As Integer
    Dim month As String
    Dim year As Integer
    Dim assembledDate As String
    Dim monthNum As Integer
    Dim tempArray() As String
    Dim pattern As String()
    Dim RegEx As Object

    dateString = inputDate
    Set RegEx = CreateObject("VBScript.RegExp")

    pattern = "(/)|(,)|(-)"
    dateStringArray() = RegEx.Split(dateString, pattern)

    ' .... code continues

这就是我目前正在做的事情。但是,RegEx.Split 函数期间似乎有问题,因为它似乎导致我的代码挂起并且无法进一步处理。

为了确认,我做了一些简单的事情:

MsgBox("Hi")
pattern = "(/)|(,)|(-)"
dateStringArray() = RegEx.Split(dateString, pattern)
MsgBox("Bye")

“Hi”msgbox 弹出,但“Bye”msgbox 永远不会弹出,更下方的代码似乎根本没有被执行,这导致我怀疑 RegEx.Split 导致它被卡住了。

我可以检查我是否真的以正确的方式使用 RegEx.Split 吗?根据 MSDN here,Split(String, String) 也返回一个字符串数组。

谢谢!

编辑:我试图不探索 CDate() 函数,因为我试图不依赖于用户计算机的区域设置。

【问题讨论】:

  • 您真的需要为此使用RegEx 吗? CDate 不符合要求吗?
  • 首先,Split 不是正则表达式的方法。其次,您链接的参考是 VB.Net,而不是 VBA。在 VBA 的 Regex 以及 Split 函数上,SO 中有很多答案。
  • @paul 由于一些复杂的原因,我试图不依赖计算机上的区域设置。但是 CDate 如何帮助拆分它呢?
  • @chrisneilsen 哦,这不是一回事吗?请原谅我自己的困惑。我一直试图在 SO for Regex Split in VBA 中找到它,但无济于事,但谢谢,我会尝试继续搜索
  • datVar = CDate("10/11/12") 然后Year(datVar), Month(datVar), Day(datVar)

标签: regex excel vba string-split


【解决方案1】:

在 VBA 中用正则表达式拆分字符串:

Public Function SplitRe(Text As String, Pattern As String, Optional IgnoreCase As Boolean) As String()
    Static re As Object

    If re Is Nothing Then
        Set re = CreateObject("VBScript.RegExp")
        re.Global = True
        re.MultiLine = True
    End If

    re.IgnoreCase = IgnoreCase
    re.Pattern = Pattern
    SplitRe = Strings.Split(re.Replace(text, ChrW(-1)), ChrW(-1))
End Function

使用示例:

Dim v
v = SplitRe("a,b/c;d", "[,;/]")

【讨论】:

  • 很好 - 但如果字符串已经包含 vbNullChar 字符,结果会很糟糕。
  • 如何返回 MsgBox (v)?我收到运行时 13 错误。类型不匹配。
  • 什么是字符串()。这是一个返回另一个字符串类型函数的函数吗?
【解决方案2】:

引用 VbScript Regexp 文档中的一个示例: https://msdn.microsoft.com/en-us/library/y27d2s18%28v=vs.84%29.aspx

Function SubMatchTest(inpStr)
    Dim retStr
    Dim oRe, oMatch, oMatches
    Set oRe = New RegExp
    ' Look for an e-mail address (not a perfect RegExp)
    oRe.Pattern = "(\w+)@(\w+)\.(\w+)"
    ' Get the Matches collection
    Set oMatches = oRe.Execute(inpStr)
    ' Get the first item in the Matches collection
    Set oMatch = oMatches(0)
    ' Create the results string.
    ' The Match object is the entire match - dragon@xyzzy.com
    retStr = "Email address is: " & oMatch & vbNewLine
    ' Get the sub-matched parts of the address.
    retStr = retStr & "Email alias is: " & oMatch.SubMatches(0)  ' dragon
    retStr = retStr & vbNewLine
    retStr = retStr & "Organization is: " & oMatch.SubMatches(1)    ' xyzzy
    SubMatchTest = retStr
End Function

要测试,请调用:

MsgBox(SubMatchTest("Please send mail to dragon@xyzzy.com. Thanks!"))

简而言之,您需要您的 Pattern 来匹配您想要提取的各个部分,并在其间使用spearators,可能类似于:

"(\d+)[/-,](\d+)[/-,](\d+)"

整个内容将在 oMatch 中,而数字 (\d) 将在 oMatch.SubMatches(0) 到 oMatch.SubMatches(2) 中结束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-23
    相关资源
    最近更新 更多