Regular Expression

Last Updated: 27 Nov 2016

This entire chapter refers to a feature that was added in version 4.18.4

The Regular Expression asset allows you to define one or more regular expression and replacement string pairs. This asset can then be used in conjunction with regular expression keyword modifiers to match and replace returned keyword values.

Bookmarks

Once you have added a Regular Expression, you can configure its settings on its associated asset screens. The majority of these screens are the same or similar to those for a Standard Page and are described in the Asset Screens manual. In this chapter, we will describe the Details screen, which is different for a Regular Expression.

Details Screen

Regular Expression

This section allows you to configure the regular expressions and replacements on the Regular Expression.

The Regex Data table allow you to define the regular expression on the Regular Expression asset. Once you have entered a regular expression in the Regex field, clicking Commit will create additional fields in this table, allowing you to set multiple regular expressions.

Regular expressions must be defined as a PCRE based patterns, wrapped with a delimiter and optional modifiers, for example:

  • /match this/
  • @/path/[a-z]+/match@
  • /match_both_cases/i
  • /multi_lines.*?/ms

Replacements can be defined in the Replacement field for each regular expression you have set. Enabling the Replace Keywords field will allow the regular expression replacement to be applied on any Global Keyword replacements in both the Regex and Replacement field values.

A maximum number of possible replacements for each regular expression can be set in the Replacement Limit field. By default, this field is set to 0, meaning that no limit has been exercised.

Keyword Modifiers

The Regular Expression asset has been designed to be used in conjunction with the following keyword modifiers:

  • preg_replace: replaces the regular expression value with the replacement set on the Regular Expression asset. This is the same functionality as the preg_replace() PHP function.
    % asset_contents^preg_replace:[REGEX_ASSETID]%
  • preg_match: returns a success/failure value if the regular expression matches the returned keyword value. This is the same functionality as the preg_match_all() PHP function.
    % asset_contents^preg_match:[REGEX_ASSETID]%
    In the above example, this keyword will return 0 if no match is found in the contents of the asset. If a match is found, this keyword will return the number of matches, e.g. 2 for two matches.
    % asset_contents^preg_match:[REGEX_ASSETID]:Yes:No%
    In this example, the keyword will return No if no match is found, and Yes if any number of matches are found.
  • preg_match_result: returns regular expression matches on the returned keyword value as an array. This is the same functionality as the preg_match_all() PHP function, and should be used in conjunction with the array keyword modifiers.
    % asset_contents^preg_match_result:[REGEX_ASSETID]^as_json%

Previous Chapter Next Chapter