Match a Sub-string with a Regular Expression in TestStand

Updated Oct 22, 2024

Environment

Software

  • TestStand

This article demonstrates how to use a Regular Expression in TestStand to match a pattern that applies to a partial or sub-string. This can be used in cases where a Regular Expression does not account for an entire string.

To identify a pattern in a partial string, use the FindPattern function to either return a Passed/Failed status or the content of the matched string. The relative sections below describe how to implement this.

 

Returning a Passed/Failed Status

  • In the Sequence, create the following Local Variables:
    • a String called Str1. Set the value to the string to be compared.
    • a String called Str2. Set the value to a Regular Expression.
    • a Number called PatternLength.
  • Place a Pass/Fail Test Step in the Sequence.
  • In the Data Source tab, enter Step.Result.PassFail. This will use the step result as the pass/fail comparison.
  • In the Properties tab, click on the Expressions option.
  • In the Pre-Expression input, enter the expression below. This will store the result of FindPattern in the step's Data Source variable.

Step.Result.PassFail = FindPattern(Locals.Str1, Locals.Str2, 0, True, Locals.PatternLength) == 1 ? False:True

 

Returning a Matched String

  • In the Sequence, create the following Local Variables:
    • a String called Str1. Set the value to the string to be compared.
    • a String called Str2. Set the value to the Regular Expression.
    • a Number called FoundIndex.
    • a Number called PatternLength.
  • Place a Statement Step.
  • Enter the following statement:

Locals.FoundIndex = FindPattern(Locals.Str1, Locals.Str2, 0, True, Locals.PatternLength)

  • Place a Message Popup Step.
  • In the Text and Buttons tab, enter the following Message Expression.
    • This uses the Mid function to retrieve the matching sub-string from Locals.Str1

"The matched string is " + Mid(Locals.Str1, Locals.FoundIndex, Locals.PatternLength)