Regular expression

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.

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 as a standard page and are described in the asset screens manual. This chapter will describe the Details screen, which is different for a regular expression asset.

Details screen

Regular expression

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

The regex data table allows you to define the regular expression on the regular expression asset. Once you have entered a regular expression in the Regex field, clicking save 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 to 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 asset’s contents. If a match is found, this keyword will return the number of matches, for example, 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 functionality is the same 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%

© 2015- Squiz Pty Ltd