Skip to main content

pattern-replace

Syntax

- pattern-replace:
pattern: regular expression
with: replacement
else: default string

Description

The pattern-replace operation allows searching for a pattern within text and replacing the part of text matching the pattern with another value.

The pattern argument is mandatory. Its value can be a simple string, such as a word or a number. The value can also be a full regular expression, allowing for more complicated matches to take place. Regular expressions should always be surrounded in a pair of forward slashes (/). The slashes won’t be considered part of the pattern itself.

The with argument is mandatory. This is the replacement for each instance of a pattern match. The entire matching value can itself be used as part of the replacement by using the symbol $0. If parentheses were used in the pattern (representing parts of the pattern), those matching parts can be used in the replacement by using symbols $1, $2, $3, etc. Numbers are assigned in the order that the parentheses groups appear from left to right.

The else argument is optional. If a match can’t be found in the input string, the result of the operation will be the value specified in else instead. If no else value is specified, then the result is the original unmodified string.

Pattern matching is an advanced technical topic. You should have familiarity with common regular expression syntax and rules if you plan to replace patterns more complex than an exact string match.

Examples

  var1:
- source: program-name
- pattern-replace:
pattern: O7001
with: O7003
info

In the above example, program-name is an identifier from another part of the adapter script.

  var1:
- source: program-name
- pattern-replace:
pattern: /O([0-9]+)/
with: $1
info

In the above example, program-name is an identifier from another part of the adapter script.

  var1:
- source: program-name
- pattern-replace:
pattern: /O([0-9]+)/
with: $1
else: 0
info

In the above example, program-name is an identifier from another part of the adapter script.