Wednesday, January 13, 2016

REGEXP for splitting filename lie "99.99.99-foo bar is good" to "99.99.99" and the rest of it... for hortening up filepaths in shortcuts


Sub testregexp()
Dim x
x = RegexpSplit("99.01.02-foo bar test")
    Debug.Print x(0)
End Sub
Function RegexpSplit(strFN As String) ''returns array of 2 items matching '###.###...' and the remainder

  Dim rgq As String
  rgq = "((?:\d+\.{0,1}){1,})(.*)"

  Dim RegEx As New RegExp
  Set RegEx = CreateObject("vbscript.regexp")
 
  RegEx.Pattern = rgq
  RegEx.IgnoreCase = True
  Dim m As MatchCollection
  Set m = RegEx.Execute(strFN)
 
  Dim x(1)
  If m.Item(0).SubMatches.count > 0 Then ''values weere found to match 2 subexpressions
    x(0) = m.Item(0).SubMatches.Item(0)
    x(1) = m.Item(0).SubMatches.Item(1)
  Else
    x(0) = strFN
    x(1) = strFN
  End If
   
    RegexpSplit = x() ''return value
   
  Set RegEx = Nothing
 
End Function

No comments:

Post a Comment