How to use regular expresions from custom code in API testing

  • KM03787414
  • 23-Feb-2021
  • 24-Feb-2021

Summary

Here is the basic implementation of the regular expression (Regex) in UFT API

Question

Notes:

- The only statement needed to make this work is the declaration:
  "Using System.Text.RegularExpressions; ..."

- The regex has nothing to do with checkpoints but from here you can use it
  define a checkpoint.Report or whatever

- The regex code can be defined on any event

Answer

Implentation:

1. Regex requires declaration at the top of the script:
using System.Text.RegularExpressions;

enter the statement at the top of script TestUserCode.cs

Example:


namespace Script
{
    using System;
     ...
    using System.Text.RegularExpressions;
    
    [Serializable()]
    public class TestUserCode : TestEntities
    {
      ...


2. Regex sample code
    The code can be defined on any event

//Define variables
string sText;
bool bRxMchFnd;   //true/false

sText = "abcd";

this.Context.UserLogger.Info(" String: '" + sText + "'");

//- Pattern found
var RxMatch = Regex.Match(sText, "bc");
bRxMchFnd = RxMatch.Success;       //True
this.Context.UserLogger.Info(" Match 'bc': " + bRxMchFnd);

//- Pattern NOT found
RxMatch = Regex.Match(sText, "xxx");
bRxMchFnd = RxMatch.Success;       //False
this.Context.UserLogger.Info(" Match 'xxx': " + bRxMchFnd);


Output

 String: 'abcd'
 Match 'bc': True
 Match 'xxx': False
 

DISCLAIMER OF WARRANTY

The example software is experimental and is provided as a courtesy, free of charge, "AS-IS" by Micro Focus. Micro Focus shall have no obligation to maintain or support this software sample. MICRO FOCUS MAKES NO EXPRESS OR IMPLIED WARRANTY OF ANY KIND REGARDING THIS SOFTWARE SAMPLE. MICRO FOCUS SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, WHETHER BASED ON CONTRACT, TORT OR ANY OTHER LEGAL THEORY, IN CONNECTION WITH OR ARISING OUT OF THE FURNISHING, PERFORMANCE OR USE OF THIS SOFTWARE SAMPLE.