String affiliation
Pattern format
A string affiliation pattern begins with a prefix and ends with a closing back-tick:
- Single dollar (
$`): Ignores insignificant whitespace during matching. - Double dollar (
$$`): Matches text exactly, preserving all whitespace.
The string affiliation pattern format is the following:
$`<text>{<placeholder>}[text]`
The following example extracts a number from a sentence:
$`price is {value:number} euros`
Placeholder syntax
Placeholders extract values and must be enclosed in braces {}. Each placeholder
defines a variable name and how to parse its value.
| Syntax | Description | Example |
|---|---|---|
{name:type} |
Uses a built-in type | {count:number} |
{name:"regex"} |
Uses a custom regular expression | {code:"[A-Z]{3}-\d{4}"} |
{name:type:format description} |
Applies formatting for dates, numbers, etc. | {date:date:yyyy-MM-dd}
|
Built-in types include Boolean, number,
string, date and time, and others. For more information about
built-in types, see Data types.
Any type that supports JSON serialization can also be used in a placeholder.
using first and using all constructs
String affiliations work with the using first and using all
constructs to extract variables from text.
using first matches the first occurrence of a pattern and declares variables
from the match. The using first construct uses the following syntax:
using first $`<text>{<placeholder>}<text>` in <input text> do
<statements>
end
The following example extracts the first number from a string:
using first $`{value: number}` in "1234567890123456 number is a long. 123 is an int." do
set result to "value: " + n;
end
Here, the expected output is value:
1234567890123456.
using all matches all occurrences of a pattern, extracting variables from each
match. The rule triggers once per successful match. The using all construct uses
the following syntax:
using all $`<text>{<placeholder>}<text>` in <input text> do
<statements>
end
The following example extracts all numbers from a string:
set result to " ";
using all $`{value: number}` in "int: 123 long: 123456789012345L hexa: 0xFFFF" do
set result to result + value + " ";
end
Here, the expected output is 123 123456789012345 65535
.