pattern-match
Syntax
Shorthand syntax
- pattern-match: regular expression
Extended syntax
- pattern-match:
pattern: regular expression
group: capture group
index: nth match
else: default string
Description
The pattern-match operation allows searching for a pattern within text and capturing the part of text matching the pattern.
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 group argument is optional, and is only used when the pattern being matched is a regular expression. Regular expressions allow using parentheses to create “capturing groups”, which are smaller patterns within a larger pattern. Capturing groups are assigned a number starting with 1 as they appear within a pattern string. Setting the group argument to the number of a capturing group will cause only that part of the pattern to be captured by the operation. By default, the entire pattern will be captured.
The index argument is optional. When specified, the match operation will look for all instances of the pattern in the value, and return the nth match as specified by the index. The index argument is also compatible with group.
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 an empty string.
Pattern matching is an advanced technical topic. You should have familiarity with common regular expression syntax and rules if you plan to capture patterns more complex than an exact string match.
Examples
- AdapterScript
- Result
var1:
- source: program-name
- pattern-match:
pattern: WARMUP1
In the above example, program-name
is an identifier from another part of the adapter script.
INPUT: CNCpart1 RESULT:
INPUT: WARMUP1 RESULT: WARMUP1
INPUT: WARMUP2 RESULT:
- AdapterScript
- Result
var1:
- source: program-name
- pattern-match:
pattern: /O7[0-9]+/
In the above example, program-name
is an identifier from another part of the adapter script.
INPUT: O8003 RESULT:
INPUT: O7005 RESULT: O7005
- AdapterScript
- Result
var1:
- source: program-name
- pattern-match:
pattern: /O(7[0-9]+)/
group: 1
else: -1
In the above example, program-name
is an identifier from another part of the adapter script.
INPUT: O8003 RESULT: -1
INPUT: O7005 RESULT: 7005