Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Is it possible to verify if a string conforms to a specific pattern using SilkTest?

Is it possible to verify if a string conforms to a specific pattern using SilkTest?

SilkTest does not contain an in-built function that can perform the outlined functionality, but using the existing functions available to a user it is possible to create your own function to evaluate if a string conforms to a specific pattern. The function below demonstrates how this functionality is achieved using the 4Test language:

[-] boolean VerifyStringPattern(String sPattern, String sMask)
[ ] //The boolean value to evaluate the pattern match
[ ] boolean retVal = false
[ ]
[ ] //get the left-most character of the mask file, are we looking
[ ]
//for a digit?
[-]
if(Left(sMask,1) == "d")
[ ] //if we are looking for a digit, is the left-most character of
[ ]
//the sPatter string a digit
[-]
if(IsDigit(Left(sPattern,1)))
[ ] //Yes, evaluate to true
[ ] retVal = true
[ ] //No, we are looking for either an alphabetic character or a
[ ]
//specific pattern character
[-] else
[ ] //Are we looking for an alphabetic character
[-]
if(Left(sMask,1) == "a")
[ ] //Yes, Is the left-most character of the sPattern string an
[ ] //
alphabetic character
[-]
if(IsAlpha(Left(sPattern,1)))
[ ] //Yes, Evaluate to true
[ ] retVal = true
[ ] //No, We are looking for a specific pattern character
[-] else
[ ] //Does the left-most character of sPattern match the
[ ] //
pattern character in the mask file
[-]
if(Left(sPattern,1) == Left(sMask,1))
[ ] //Yes, evaluate to true
[ ] retVal = true
[ ]
[ ] //left-most character is now evaluated, strip the sPattern and
[ ] //
sMask strings of their left-mostcharacter and check that
[ ] // the next character in sPattern conforms to the mask string
[-]
if(retVal == true && Len(sPattern) != 1)
[ ] //if at any stage a character does not conform to the mask
[ ] //
pattern, evaluate to false
[-]
if(!VerifyStringPattern(Right(sPattern,Len(sPattern)-1), Right(sMask,Len(sMask)-1)))
[ ] retVal =
false
[ ]
[ ]
//return the evaluation of this function
[ ]
return retVal


In the above function, the "sPattern" argument is the string variable you pass to the function to verify if it conforms to a specific pattern. The "sMask" argument is the string variable that contains the pattern that you want to match the "sPattern" function to, the "sMask" argument must take a format such as the following examples:

  • "aaa-ddd"
  • "ddd-ddd-dddd"
  • "ddaaaadd"

In the above examples, the "a" specifies that this character must be an alphabetic character, the "d" specifies that the character must be a digit. You can also specify other characters within your pattern and the function will evaluate if that character appears in the string you wish to verify.

The below demonstrates a testcase which implements the use of the VerifyStringPattern function:

[-] testcase StringPattern() appstate none
[-] if(VerifyStringPattern("abc-123","aaa-ddd"))
[ ] print("Pattern Matched")
[-] else
[ ] print("Pattern Mismatch")
[ ]

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:35
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.