使用RegEx在VBA中分割字符串 [英] Splitting String in VBA using RegEx
问题描述
我是VBA的新人,想要使用RegEx寻求一些帮助,我希望以某种方式启发我在做错什么。我正在尝试将日期分割成其日期,月份和年份,可能的分隔符包括,, - 和/\".
函数formattedDate(inputDate As String)As String
pre>
Dim dateString As String
Dim dateStringArray()As String
Dim day As Integer
Dim month As String
Dim year As Integer
Dim decoratedDate As String
Dim monthNum As Integer
Dim tempArray()As String
Dim pattern As String()
Dim RegEx As Object
dateString = inputDate
设置RegEx = CreateObject(VBScript.RegExp)
pattern =(/)|(, )|( - )
dateStringArray()= RegEx.Split(dateString,pattern)
'....代码继续
这是我目前正在做的。然而,在RegEx.Split函数中似乎有问题,因为它似乎导致我的代码挂起而不能进一步处理。
为了确认,我做了简单的一点:
MsgBox(Hi)
pattern =(/)|(,)|( - )
dateStringArray()= RegEx.Split(dateString,pattern)
MsgBox(Bye)
嗨msgbox弹出,但再见msgbox从来没有弹出,代码进一步下降似乎根本不被排除,这导致我怀疑RegEx.Split导致它被卡住。
我可以检查我是否正确使用RegEx.Split?根据MSDN here ,拆分( String,String)返回一个字符串数组。
谢谢!
/ strong>:我试图不去探索CDate()函数,因为我试图不依赖于用户电脑的区域设置。
解决方案在VBA中使用正则表达式拆分字符串:公共函数SplitRe(text As String, pattern As String,可选的ignorecase As Boolean)As String()
静态重新作为对象
如果re是Nothing然后
设置re = CreateObject(VBScript.RegExp)
re .Global = True
re.MultiLine = True
End If
re.ignorecase = ignorecase
re.pattern = pattern
SplitRe = Strings.Split(re.Replace (文本,vbNullChar),vbNullChar)
结束函数
使用示例:
Dim v
v = SplitRe(a,b / c; d,[,; /])
I'm new to VBA and would like to seek some help with regards to using RegEx and I hope somehow can enlighten me on what I'm doing wrong. I'm currently trying to split a date into its individual date, month and year, and possible delimiters include "," , "-" and "/".
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
This is what I am currently doing. However, there seems to be something wrong during the RegEx.Split function, as it seems to cause my codes to hang and not process further.
To just confirm, I did something simple:
MsgBox("Hi") pattern = "(/)|(,)|(-)" dateStringArray() = RegEx.Split(dateString, pattern) MsgBox("Bye")
"Hi" msgbox pops out, but the "Bye" msgbox never gets popped out, and the codes further down don't seem to get excuted at all, which led to my suspicion that the RegEx.Split is causing it to be stuck.
Can I check if I'm actually using RegEx.Split the right way? According to MSDN here, Split(String, String) returns an array of strings as well.
Thank you!
Edit: I'm trying not to explore the CDate() function as I am trying not to depend on the locale settings of the user's computer.
解决方案To split a string with a regular expression in 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, vbNullChar), vbNullChar) End Function
Usage example:
Dim v v = SplitRe("a,b/c;d", "[,;/]")
这篇关于使用RegEx在VBA中分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文