Ruslan Ulanov’s Codeshack

The developer’s notebook

Regular Expressions in InstallScript

2 comments

If you’ve ever had to parse strings in InstallShield’s InstallScript, you’d know that a few of InstallShield’s built-in String Functions might not be enough. Regular Expressions are a way to go, but unfortunatelly Acresso didn’t implement them (yet?) in their product.

This post from Acresso Community might have a solution for you (at least until Acresso will figure out a way to implement RegEx natively in their products).

Problems calling VBScript/COM objects from InstallScript
Dated: 07-14-2006, 11:17 AM

Can anyone tell whats wrong with this function?
It is supposed to create an VBscript Regexp object and then test a string against the expression and return the result of the test (TRUE/FALSE).
It always returns FALSE, even when it obviously should be TRUE – tested a simple expression. Am I missing something in how to call VBScript/COM objects and test return codes?

Code:

prototype BOOL ValidateString(STRING, STRING); 
function BOOL ValidateString(szString,szPattern)
OBJECT oRegEx, oMatch;  
BOOL MatchFound;
begin

	//try to create the RegEx object
	try
		set oRegEx = CreateObject("VBScript.RegExp");
	catch
		MessageBox("CreateObject Failed- "+ Err.Decription, SEVERE);
	endcatch;

	oRegEx.Pattern = szPattern;
	oRegEx.IgnoreCase = 0;
	oRegEx.Global = 1;  //set to 1 to find all matches in the string
			//set to 0 to find only the first match
	try
		set oMatch = oRegEx.Execute(szString);
	catch
		MessageBox("CreateObject Failed- "+ Err.Decription, SEVERE);
	endcatch;

	MatchFound = (oMatch.Count > 0);
	return MatchFound;
end;

Actually, that was a second attempt. The initial attempt was to call the Test method of REGEX directly, but that didn’t work either.

Code:
prototype BOOL ValidateString(STRING, STRING); 
function BOOL ValidateString(szString,szPattern)
OBJECT oRegEx;  
BOOL MatchFound;
begin

	//try to create the RegEx object
	try
		set oRegEx = CreateObject("VBScript.RegExp");
	catch
		MessageBox("CreateObject Failed- "+ Err.Decription, SEVERE);
	endcatch;

	oRegEx.Pattern = szPattern;
	oRegEx.IgnoreCase = 0;
	oRegEx.Global = 1;  //set to 1 to find all matches in the string
			//set to 0 to find only the first match
	MatchFound = oRegEx.Test(szString);
	return MatchFound;
end;

This is being #included into a setup.rul in an InstallScriptMSI project.

 Solved it!!
Dated: 07-14-2006, 01:09 PM

Turns out I only needed to declare the Pattern variable as a “VARIANT”.
Can be taken care of in the PROTOTYPE declaration:

Code:
prototype BOOL ValidateString(STRING, VARIANT);

The second set of code works great as a regular expression tester if anyone else is looking for one. Use the above prototype and this code:

Code:
function BOOL ValidateString(szString,szPattern)
OBJECT oRegEx;   
BOOL MatchFound;
begin

	//try to create the RegEx object
	try
		set oRegEx = CoCreateObject("VBScript.RegExp");
	catch
		MessageBox("CoCreateObject Failed- "+ Err.Decription, SEVERE);
	endcatch;
	oRegEx.Pattern = szPattern;
	oRegEx.IgnoreCase = 0;
	MatchFound = oRegEx.Test(szString);  
	set oRegEx = NOTHING;
	return MatchFound;
end;

The original forum thread started by user Rincewind could be found here.

Written by Ruslan Ulanov

July 20th, 2009 at 11:16 am

Posted in Installer,VBScript

Tagged with