![]() |
|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Problems calling VBScript/COM objects from InstallScript
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;
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;
|
|
#2
|
||||
|
||||
|
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); 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;
|
|
#3
|
||||
|
||||
|
cant call the function!
I have tried your solution to validate a string
my regular expression is defined like so: #define PATTERN "^[\w]*$" That should match any string of alphanumeric characters, which is what I want. I have tried calling the ValidateString method several different ways, in each instance it always returns FALSE. How do you call the ValidateString method? |
|
#4
|
||||
|
||||
|
Must use a STRING variable
Quote:
But, a check in some older code showed the following: Code:
STRING emailPattern;
emailPattern="\w+@\w+\.\w+";
...
elseif (!ValidateString(svMailAddr,emailPattern)) then
MessageBox ("Invalid format for an email address.", WARNING);
dlgCtrl = GetDlgItem (hwndDlg, RES_MAIL_ADDR);
SetFocus (dlgCtrl);
CtrlSelectText (szDialogName, RES_MAIL_ADDR);
...
Make sure you are using the version from my corrected post and you've declared the PROTOTYPE correctly. Last edited by Rincewind : 06-08-2007 at 02:02 PM. Reason: Adding a comment |
|
#5
|
||||
|
||||
|
Watch your quoting and slashes
When generating your regular expression string, remember that Installscript will impose some requirements on your string:
embeded quotes must be escaped with a backslash (\)My string in the previous post wouldn't work because the backslashes were only singled, not doubled. That is, it should have been: Code:
emailPattern="\\w+@\\w+\\.\\w+"; Code:
// real pattern: ^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})$
emailPattern='^(([^<>()[\\]\\\\.,;:\\s@\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\"]+)*)|(\\".+\\"))@(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,})$';
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|