Between
between returns the part of a string found between two markers.
Overview
Use between to extract text enclosed by an opening and closing marker:
message | "Welcome, [Amanda]!" name | message.between("[", "]") :say(name) /// Amanda
message | "Welcome, [Amanda]!" name | message.between("[", "]") :say(name) /// Amanda
The markers themselves are not included in the result.
Method Style
Call between as a method on the string you want to search:
record | "order: {A-1842}" order_number | record.between("{", "}") :say(order_number) /// A-1842
record | "order: {A-1842}" order_number | record.between("{", "}") :say(order_number) /// A-1842
The equivalent free-call form is:
order_number | between("order: {A-1842}", "{", "}")
order_number | between("order: {A-1842}", "{", "}")
Both forms behave the same way.
First Matching Pair
between uses the first left marker and then the first right marker that appears after it:
text | "[north] and [south]" text.between("[", "]") /// north
text | "[north] and [south]" text.between("[", "]") /// north
It returns one string, not every enclosed section.
Multi-Character Markers
The markers may contain more than one character:
template | "Hello, {{customer_name}}!" field | template.between("{{", "}}") :say(field) /// customer_name
template | "Hello, {{customer_name}}!" field | template.between("{{", "}}") :say(field) /// customer_name
Missing Markers
If the left marker is missing, between returns an empty string:
between("Welcome, Amanda!", "[", "]") /// ""
between("Welcome, Amanda!", "[", "]") /// ""
If the left marker is found but no right marker follows it, the result is also an empty string:
between("Welcome, [Amanda!", "[", "]") /// ""
between("Welcome, [Amanda!", "[", "]") /// ""
Marker Order
The right marker must appear after the selected left marker. An earlier right marker is ignored:
between("] old [new]", "[", "]") /// new
between("] old [new]", "[", "]") /// new
Signature
string.between(left, right) between(string, left, right)
string.between(left, right) between(string, left, right)
| Argument | Type | Description |
|---|---|---|
string |
string | The text to search. |
left |
string | The marker immediately before the wanted text. |
right |
string | The marker immediately after the wanted text. |
between returns a string.
Errors
between requires a string and two markers:
between("Welcome, [Amanda]!", "[") /// error: R0301 wrong-arity
between("Welcome, [Amanda]!", "[") /// error: R0301 wrong-arity
All three values must be strings:
between("Chapter 12", "Chapter ", 20) /// error: T0205 type-mismatch
between("Chapter 12", "Chapter ", 20) /// error: T0205 type-mismatch