Attribute VB_Name = "RegExps" Option Explicit 'Regular Expression wrappers 'by Toby Erkson '14Feb2007 'http://www.regular-expressions.info/vbscript.html 'http://msdn.microsoft.com/library/en-us/script56/html/9f1c25ba-46ce-46af-9f19-ac1d2bcf05d8.asp?frame=true 'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/711116fb-9c47-47cb-b664-db8141b8cc69.asp 'This Module requires Early Binding (minimum version) = "Microsoft VBScript Regular Expressions 5.5" Function RegExp_Replace(ReplaceIn, sPattern As String, ReplaceWith As String, Optional IgnoreCase As Boolean = False, _ Optional GlobalMatch As Boolean = False, Optional bMultiLine As Boolean = False) 'This is the Replace method. 'Returns a copy of ReplaceIn with the text of sPattern replaced with ReplaceWith. 'If no match is found, a copy of ReplaceIn is returned unchanged. Dim RE As RegExp Set RE = New RegExp RE.Pattern = sPattern RE.IgnoreCase = IgnoreCase RE.Global = GlobalMatch RE.MultiLine = bMultiLine RegExp_Replace = RE.Replace(ReplaceIn, ReplaceWith) End Function Function RegExp_Match(FindIn, sPattern As String, Optional IgnoreCase As Boolean = False, _ Optional GlobalMatch As Boolean = False, Optional bMultiLine As Boolean = False) As Variant 'This is the Execute method. 'Returns a Matches collection containing a Match object for each match found in string. 'Execute returns an empty Matches collection if no match is found. The object returned is the location in the string. Dim i As Long, RE As RegExp, allMatches As MatchCollection, aMatch As Match Set RE = New RegExp RE.Pattern = sPattern RE.IgnoreCase = IgnoreCase RE.Global = GlobalMatch RE.MultiLine = bMultiLine Set allMatches = RE.Execute(FindIn) ReDim Results(0 To allMatches.Count - 1) For i = 0 To allMatches.Count - 1 Results(i) = allMatches(i).FirstIndex Next i RegExp_Match = Results End Function Function RegExp_ShowMatch(FindIn, sPattern As String, Optional IgnoreCase As Boolean = False, _ Optional GlobalMatch As Boolean = False, Optional bMultiLine As Boolean = False) As Variant 'This is the Execute method. 'Returns a Matches collection containing a Match object for each match found in string. 'Execute returns an empty Matches collection if no match is found. The object returned is the found string itself. Dim i As Long, RE As RegExp, allMatches As MatchCollection, aMatch As Match Set RE = New RegExp RE.Pattern = sPattern RE.IgnoreCase = IgnoreCase RE.Global = GlobalMatch RE.MultiLine = bMultiLine Set allMatches = RE.Execute(FindIn) ReDim Results(0 To allMatches.Count - 1) For i = 0 To allMatches.Count - 1 Results(i) = allMatches(i) Next i RegExp_ShowMatch = Results End Function Function RegExp_Exists(FindIn, sPattern As String, Optional IgnoreCase As Boolean = False, _ Optional GlobalMatch As Boolean = False, Optional bMultiLine As Boolean = False) As Boolean 'This is the Test method. 'Returns True if a pattern match is found; False if no match is found. Dim i As Long, RE As RegExp, allMatches As MatchCollection, aMatch As Match Set RE = New RegExp RE.Pattern = sPattern RE.IgnoreCase = IgnoreCase RE.Global = GlobalMatch RE.MultiLine = bMultiLine RegExp_Exists = RE.test(FindIn) End Function