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
- AdapterScript
- Result
var1:
- source: program-name
- pattern-replace:
pattern: O7001
with: O7003
In the above example, program-name
is an identifier from another part of the adapter script.
INPUT: O8000 RESULT: O8000
INPUT: O7001 RESULT: O7003
- AdapterScript
- Result
var1:
- source: program-name
- pattern-replace:
pattern: /O([0-9]+)/
with: $1
In the above example, program-name
is an identifier from another part of the adapter script.
INPUT: O9013 RESULT: 9013
INPUT: OX123 RESULT: OX123
- AdapterScript
- Result
var1:
- source: program-name
- pattern-replace:
pattern: /O([0-9]+)/
with: $1
else: 0
In the above example, program-name
is an identifier from another part of the adapter script.
INPUT: O9013 RESULT: 9013
INPUT: OX123 RESULT: 0