Just several functions you may find useful working in VB with regular expressions:
Code: Select all
Function RegExpReplace(patrn, strng, strtoReplace)
Dim regEx
Set regEx = New RegExp ' Create regular expression.
regEx.Global = True ' Set global applicability.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive.
'''''''MsgBox strng
'''''''MsgBox strtoReplace
On Error Resume Next
RegExpReplace = regEx.Replace(strng, strtoReplace) ' Make replacement.
If Err.Number <> 0 Then
Err.Clear
RegExpReplace = ""
End If
End Function
Function RegExpReplace(patrn, strng, strtoReplace)
Dim regEx
Set regEx = New RegExp ' Create regular expression.
regEx.Global = True ' Set global applicability.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive.
'''''''MsgBox strng
'''''''MsgBox strtoReplace
On Error Resume Next
RegExpReplace = regEx.Replace(strng, strtoReplace) ' Make replacement.
If Err.Number <> 0 Then
Err.Clear
RegExpReplace = ""
End If
End Function
'(As you see these functions safely
'return empty string if not found anything.)
Function RegExpTest(patrn, strng)
Dim regEx, retVal ' Create variable.
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Set case sensitivity.
On Error Resume Next
retVal = regEx.Test(strng) ' Execute the search test.
If Err.Number <> 0 Then
Err.Clear
RegExpTest = False
End If
RegExpTest = retVal
End Function
'How you can use it:
strIni = RegExpRetrieve("\[" & safePtrn & "\]((?:.|\s)*?)\[/" & safePtrn & "\]", strText)
strFileText = RegExpReplace("\[Field\]((?:.|\s)*?)\[\/Field\]", strFileText, "[Field]" & strNewValue & "[/Field]")
Sub HasNonSpaceSymbols(strng)
GlobalReturnStr = RegExpTest("[^\s]", strng)
End Sub
Sub ContractDuplicateCrlfs(newStr)
GlobalReturnStr = RegExpReplace( "[\n\r]+", newStr, vbCrlf )
End Sub
'or here is how you can parse a command line using it:
Dim GlobalReturnStr
GlobalReturnStr = ""
Function GetResultString()
GetResultString = GlobalReturnStr
End Function
Sub ReadCommandLine( commandLine )
Dim restCommandLine, mostIdx, idxLoop, ResStr, mayBeParam, numCharsToThrow, idxRest
restCommandLine = commandLine
ResStr = ""
mayBeParam = ""
mostIdx = len(restCommandLine)
For idxLoop = 0 to mostIdx
mayBeParam = RegExpRetrieve("^""([^""]+)""", restCommandLine)
if len(mayBeParam) = 0 Then
mayBeParam = RegExpRetrieve("^([^ ]+)", restCommandLine)
numCharsToThrow = len(mayBeParam)
else
numCharsToThrow = len(mayBeParam) + 2
end if
idxRest = len(restCommandLine) - numCharsToThrow - 1
if idxRest > 0 Then
restCommandLine = Trim(Right(restCommandLine,idxRest))
else
restCommandLine = ""
end if
If len(restCommandLine) = 0 Then
ResStr = ResStr & mayBeParam
Exit For
else
ResStr = ResStr & mayBeParam & vbCrlf
end if
Next
GlobalReturnStr = ResStr
End Sub
'calling it in Macro Scheduler script:
VBRun>ReadCommandLine,COMMAND_LINE
VBEval>GetResultString(),result
Separate>result,CRLF,ar_Params
'then just analyze the array...
Best,
Olga.
P.S. Why "textarea" doesn't work? I had to use "code" instead...