My contribution: working with regular expressions

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

My contribution: working with regular expressions

Post by OlgaFB » Fri Aug 04, 2006 2:59 pm

Hi All,

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...
Why I recommend to use procedures instead of functions, I think deserves to be explained in a separate tip. :)

Best,
Olga.

P.S. Why "textarea" doesn't work? I had to use "code" instead...

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts