J
jg
If I need to reuse regeex pattern or let non dotnet com client to reuse the
pattern to match against different strings, what is the best way?
How should I free up the resources held by regex?
Right now, I am leaning towards this way
Private Dim RegexCollection as New Microsoft.VisualBasic.Collection()
Private Function makekey(ByVal strPrefx As String) As String
return strPrefx + ToString(Microsoft.VisualBasic.DateAndTime.Timer) +
ToString(Rnd())
End Function
Public function getRegex(strPattern As String, ByRef strKey as String) As
Object ' leaning to use the key to deal with collection
strKey = makekey("regex")
RegexCollection .add(New Regex(strPattern), strKey)
return RegexCollection (strKey)
End Function
Public Function free_RegexObj(ByRef myobj As Object, ByRef mykey As
String)
Try
RegexCollection .Remove(mykey)
myobj.destroy()
mykey = Nothing
Return 0
Catch ex As Exception
Return -1
End Try
End Function
Public function getMatch(str2search As String, ioffset As Integer,
strRegexkey as String, byRef strRegexObj as Object,
ByRef iFoundPos as Integer. ByRef iFoundLen as integer) as integer
.................
' if found return 1 and....
....
' otherwise
return -1
End Function
Is there a better way?
pattern to match against different strings, what is the best way?
How should I free up the resources held by regex?
Right now, I am leaning towards this way
Private Dim RegexCollection as New Microsoft.VisualBasic.Collection()
Private Function makekey(ByVal strPrefx As String) As String
return strPrefx + ToString(Microsoft.VisualBasic.DateAndTime.Timer) +
ToString(Rnd())
End Function
Public function getRegex(strPattern As String, ByRef strKey as String) As
Object ' leaning to use the key to deal with collection
strKey = makekey("regex")
RegexCollection .add(New Regex(strPattern), strKey)
return RegexCollection (strKey)
End Function
Public Function free_RegexObj(ByRef myobj As Object, ByRef mykey As
String)
Try
RegexCollection .Remove(mykey)
myobj.destroy()
mykey = Nothing
Return 0
Catch ex As Exception
Return -1
End Try
End Function
Public function getMatch(str2search As String, ioffset As Integer,
strRegexkey as String, byRef strRegexObj as Object,
ByRef iFoundPos as Integer. ByRef iFoundLen as integer) as integer
.................
' if found return 1 and....
....
' otherwise
return -1
End Function
Is there a better way?