如何使用正则表达式在访问vba中分割多个分隔符

发布于 2024-12-12 00:38:57 字数 899 浏览 0 评论 0原文

我在弄清楚如何在 access vba 中使用正则表达式分割多个分隔符时遇到问题。我想采用一个字符串“This;is,really:annoying”和一个由管道(;|,)分隔的用户定义的分隔符,以便我获得以下结果“This”,“is”,“really:烦人”无论我做什么我都无法让它发挥作用。在 python 中,我只会使用 re.split,但据我所知,vba 没有这样的选项。我已经发布了我尝试过的代码:

 Private Sub Splitter(ByVal UnmodText As String, ByVal SplitDelimiters As String)
   Dim SplitExp As New RegExp
   Dim SplitMatches As MatchCollection
   Dim SplitMatch As Match
   SplitExp.IgnoreCase = True
   SplitExp.Global = True
   'SplitExp.Pattern = ".(" & SplitDelimiters & ")" & "|(?<=" & SplitDelimiters & ").$"
   'SplitExp.Pattern = ".{0,}(?=(" & SplitDelimiters & "))"
   SplitExp.Pattern = "(?!(" & SplitDelimiters & "){2})"
   MsgBox SplitExp.Pattern
   Set SplitMatches = SplitExp.Execute(UnmodText)
   For Each SplitMatch In SplitMatches
       MsgBox SplitMatch.Value
   Next
 End Sub

任何帮助将不胜感激!

I am having issues figuring out how to split on multiple delimiters using regular expressions in access vba. I want to take a string say "This;is,really:annoying" and a tring user-defined delimiters separated by pipes (;|,) so that I would obtain the following result "This" , "is", "really:annoying" No matter what I do I cannot get this to work. In python I would just use re.split but vba has no such option that I know of. I have posted the code that I have tried:

 Private Sub Splitter(ByVal UnmodText As String, ByVal SplitDelimiters As String)
   Dim SplitExp As New RegExp
   Dim SplitMatches As MatchCollection
   Dim SplitMatch As Match
   SplitExp.IgnoreCase = True
   SplitExp.Global = True
   'SplitExp.Pattern = ".(" & SplitDelimiters & ")" & "|(?<=" & SplitDelimiters & ").$"
   'SplitExp.Pattern = ".{0,}(?=(" & SplitDelimiters & "))"
   SplitExp.Pattern = "(?!(" & SplitDelimiters & "){2})"
   MsgBox SplitExp.Pattern
   Set SplitMatches = SplitExp.Execute(UnmodText)
   For Each SplitMatch In SplitMatches
       MsgBox SplitMatch.Value
   Next
 End Sub

Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2024-12-19 00:38:57

这将返回一个变量数组,给定一个要分割的字符串,以及一个包含管道分隔符的字符串。

Function SplitPlus(val As String, seps As String) As Variant
    Dim x As Integer, arrSeps As Variant
    Dim lb As Integer, ub As Integer

    arrSeps = Split(seps, "|")
    lb = LBound(arrSeps)
    ub = UBound(arrSeps)

    If ub > lb Then
        For x = lb + 1 To ub
            val = Replace(val, arrSeps(x), arrSeps(lb))
            'Debug.Print val
        Next x
    End If
    SplitPlus = Split(val, arrSeps(lb))
End Function

This will return a variant array given a string to split, and a string containing pipe-delimited separators.

Function SplitPlus(val As String, seps As String) As Variant
    Dim x As Integer, arrSeps As Variant
    Dim lb As Integer, ub As Integer

    arrSeps = Split(seps, "|")
    lb = LBound(arrSeps)
    ub = UBound(arrSeps)

    If ub > lb Then
        For x = lb + 1 To ub
            val = Replace(val, arrSeps(x), arrSeps(lb))
            'Debug.Print val
        Next x
    End If
    SplitPlus = Split(val, arrSeps(lb))
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文