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 match wildcards in 4test and get the wildcard matches?

Is it possible to match wildcards in 4test and get the wildcard matches?

Problem:

Is it possible to match wildcards in 4test and get the wildcard matches?

Resolution:

QUESTION
----------------------------------

I want to search for a specific pattern, including wildcards, in a string and return the matched portion of the string. Is this possible?

ANSWER
----------------------------------

SilkTest contains a function called MatchStr() to do a wildcard search on a string, however this function can only return TRUE or FALSE letting you know if the pattern was matched in the string.

This is obviously limiting when you wish to find what it was that was matched.

This zip file  StringPatternMatch.zip contains two files:

  • stringpatternmatch.inc - StringPatternMatch() function
  • stringpatternmatch.t - Testcase with some examples


Syntax is:

STRING sNeedle, sHaystack, sMatched
LIST OF STRING lsMatches
sMatched = StringPatternMatch(sNeedle, sHaystack [, lsMatches])


Where

  • sNeedle is your search pattern which can include * or ? wildcards. ? Matches a single character and * matches one or more.
  • sHaystack is the string in which you are looking for the sNeedle.
  • The optional third argument lsMatches is returned from the function and contains a list of the wildcards and their associated matches. e.g. { "?", "a", "*", "word" }


The function returns the string sMatched which will contain the matched string from haystack, or a blank string if there was no match.

Examples:

  1. "> [ ] // Simple match [ ] pat = "b*" [ ] str = "abcde" [ ] ret = StringPatternMatch(pat, str) [ ] Print("Searching for "{pat}" in "{str}"") [ ] Print("Return value: {ret}") Results: [ ] Searching for "b*" in "abcde" [ ] Return value: bcde

  2. "> [ ] // Simple match [ ] pat = "b*d" [ ] str = "abcde" [ ] ret = StringPatternMatch(pat, str) [ ] Print("") [ ] Print("Searching for "{pat}" in "{str}"") [ ] Print("Return value: {ret}") Results: [ ] Searching for "b*" in "abcde" [ ] Return value: bcd

  3. "> [ ] pat = "is the * of ?ur *." [ ] str = "Now is the winter of our discontent." [ ] ret = StringPatternMatch(pat, str, lsMatches) [ ] Print("") [ ] Print("Searching for "{pat}" in "{str}"") [ ] Print("Return value: {ret}") [ ] ResPrintList("Matches for the pattern "{pat}":", lsMatches) Results: [ ] Searching for "is the * of ?ur *." in "Now is the winter of our discontent." [ ] Return value: is the winter of our discontent. [-] Matches for the pattern "is the * of ?ur *.": [ ] * [ ] winter [ ] ? [ ] o [ ] * [ ] discontent



Related documents:


Old KB# 21711

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:23
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.