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 — for example, /O[0-9]+/. Regex flags can be appended after the closing slash:
/pattern/flags. Supported flags are i (case-insensitive), m (multiline), s (single-line, where .\ matches
newlines), and u (unicode). If a regular expression is syntactically invalid, the adapter will report a configuration
error at startup. The pattern can also be a string expression using ${} syntax
to embed dynamic values — for example, ${prefix}WARMUP.
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 and can also be a string expression. If a match can’t be found in the input string, the result of the operation will be the value specified in else instead. The else value can be any value, including the number 0. If else is not specified at all, 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