如何使用正则表达式在访问vba中分割多个分隔符
我在弄清楚如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将返回一个变量数组,给定一个要分割的字符串,以及一个包含管道分隔符的字符串。
This will return a variant array given a string to split, and a string containing pipe-delimited separators.