Match a Sub-string with a Regular Expression in TestStand

Updated Jul 23, 2026

Environment

Software

  • TestStand

This article demonstrates how to use a regular expression in TestStand to identify a pattern within a larger string. This approach is useful when the regular expression matches only part of the string rather than the entire content. TestStand's FindPattern() function can be used to either:

 

  1. Verify whether a match exists and return a Pass/Fail result.
  2. Extract and return the matching substring.

 

These techniques are useful when parsing serial numbers, instrument responses, log files, or any text containing embedded data.

 

Example Scenario

 

Assume a device returns the following string:

Device SN: ABC12345

And you want to identify the serial number using the following regular expression:

ABC\d+


Expected match:


ABC12345

 


Since the pattern only matches a portion of the string, the FindPattern() function can be used to locate the matching substring.

Method 1: Return a Pass/Fail Result

 

Use this approach when you only need to determine whether the specified pattern exists within the string.

 

Steps

 

 

  1. Create the following Local Variables:
    1. SerialNumber (String): The string to search.
    2. Pattern (String): The regular expression to match.
    3. PatternLength (Number): Stores the length of the matched pattern. This is an output variable required by the FindPattern() function.
  2. Add a Pass/Fail Test step to the sequence.
    1. Use the <None> adapter for this test.
  3. Open the Data Source tab and enter:

    FindPattern(Locals.SerialNumber, Locals.Pattern, 0, True, Locals.PatternLength) >= 0

  4. Run the sequence.
  5. Review the step result:
    1. Pass indicates the pattern was found.
    2. Fail indicates the pattern was not found.

 

 

Testing the sequence - Pass case

 

  1. Manually set the input variables as shown below:

    VariableValue
    Locals.SerialNumberDevice SN: ABC12345
    Locals.PatternABC\d+


  2. Place a breakpoint right after your Pass/Fail step
  3. Click Execute>>Run MainSequence.
  4. See that your step passed.


     

 

 

Testing the sequence - Fail Case

 

  1. Manually set the input variables as shown below:

    VariableValue
    Locals.SerialNumberDevice SN: AXC12345
    Locals.PatternABC\d+


  2. Place a breakpoint right after your Pass/Fail step
  3. Click Execute>>Run MainSequence.
  4. See that your step failed.

     

 

 

Method 2: Return the Matched String

 

Use this approach when you need to retrieve, display, or further process the matching text.

 

Steps

  1. Create the following Local Variables:
    1. SerialNumber (String): The string to search.
    2. Pattern (String): The regular expression.
    3. FoundIndex (Number): Stores the starting location of the match.
    4. PatternLength (Number): Stores the length of the matched string.
  2. Add a Statement step.
  3. Enter the following statement:

    Locals.FoundIndex = FindPattern(Locals.SerialNumber, Locals.Pattern, 0, True, Locals.PatternLenght)

  4. Open a Message Popup step.
  5. Open the Text and Buttons tab.
  6. Enter the following Message Expression:

    "The macthed string is " + Mid(Locals.SerialNumber, Locals.FoundIndex, Locals.PatternLenght)

 

Testing the sequence

 

  1. Manually set the input variables as shown below:

    VariableValue
    Locals.SerialNumberDevice SN: ABC12345
    Locals.PatternABC\d+


  2. Remove any breakpoints that may exist in your sequence.
  3. Click Execute>>Run MainSequence.
  4. The Message Popup displays the matching substring returned from the original string.

     

 

 

How It Works

 

The FindPattern() function searches a string for a regular expression match and returns the starting index of the first match. The function can also populate a variable with the length of the matched text. This information can then be used with the Mid() function to extract the matching substring from the original string.