extractBetween — Extract substrings between boundary markers in MATLAB and RunMat.
extractBetween(text, start, stop) returns text between two markers. Numeric positions accept single, double, and all eight integer classes as one-based structural controls.
Syntax
newText = extractBetween(str, start, end)
newText = extractBetween(str, start, end, Name, Value, ...)Inputs
str- Type
Any- Required
- Yes
- Description
- Input text scalar/array/cell.
start- Type
Any- Required
- Yes
- Description
- Start boundary marker text or positive integer position(s).
end- Type
Any- Required
- Yes
- Description
- End boundary marker text or positive integer position(s).
Name- Type
StringScalar- Required
- Yes
- Description
- Option name (`Boundaries`).
Value- Type
Any- Required
- Variadic
- Description
- Option value and additional Name/Value pairs.
Returns
newText- Type
Any- Description
- Extracted text preserving scalar/array/cell text container semantics.
Errors
RunMat:extractBetween:InvalidInput- When
- First argument is not a string array, character array, or cell array of text scalars.
- Message
extractBetween: first argument must be a string array, character array, or cell array of character vectors
RunMat:extractBetween:BoundaryType- When
- Start/end boundaries are mixed text/numeric domains or use unsupported boundary types.
- Message
extractBetween: start and end arguments must both be text or both be numeric positions
RunMat:extractBetween:PositionType- When
- Numeric boundary positions are not positive finite integers.
- Message
extractBetween: position arguments must be positive integers
Text and boundaries
extractBetween(str,start,stop) accepts two text/pattern markers or two numeric positions. A string input produces strings; a character-vector input produces a cell containing a character vector; a cell input preserves its cell layout and element text kinds. Row-wise character matrices are a RunMat extension.
Text markers are excluded by default. Numeric endpoint positions are included by default. The Boundaries option selects inclusive or exclusive handling explicitly. The first start marker and the first following stop marker determine each result; an unmatched marker produces empty text in the current implementation.
Documented boundary arrays are scalar or the same size as the text input. RunMat mode additionally permits compatible broadcasting for string output. Positions use exact one-based integer parsing. Mixed text and numeric boundary domains are rejected.
Typed missing subject or marker elements propagate a missing string result. Every literal spelling, including <missing>, is ordinary text. Independent MATLAB parity for missing-marker overloads remains unqualified.
Placement and effects
Extraction executes on host text without a GPU kernel or fusion route. Numeric or resident text input is rejected before provider access. RunMat mode can admit a resident numeric position control and gather it before host extraction; MATLAB-compatible mode rejects that extension before gathering.
The operation is pure, may throw on invalid inputs, options or output construction, and may suspend while gathering an admitted resident position. It does not access the workspace, filesystem, clock, randomness, or callbacks.
Examples
Extract between text markers
txt = "RunMat accelerates MATLAB workloads";
segment = extractBetween(txt, "RunMat ", " workloads")Expected output:
segment = "accelerates MATLAB"Include text markers
path = "snapshots/run/fusion.mat";
withMarkers = extractBetween(path, "snapshots/", ".mat", "Boundaries", "inclusive")Expected output:
withMarkers = "snapshots/run/fusion.mat"Use one-based numeric positions
name = "Accelerator";
middle = extractBetween(name, 3, 7)Expected output:
middle = "celer"Apply scalar markers to an array
files = ["runmat_accel.rs", "runmat_gc.rs"; "runmat_plot.rs", "runmat_cli.rs"];
stems = extractBetween(files, "runmat_", ".rs")Expected output:
stems = ["accel", "gc"; "plot", "cli"]Extract row by row from a character matrix
chars = char("Device<GPU>", "Planner<Fusion>");
tokens = extractBetween(chars, "<", ">")Expected output:
tokens = ['GPU '; 'Fusion']Preserve cell element text kinds
C = {'<missing>', 'A<B>C'; "Planner <Fusion>", "Device<GPU>"};
out = extractBetween(C, "<", ">")Expected output:
out = {'missing', 'B'; "Fusion", "GPU"}Propagate missing string values
txt = [missing, "Planner<GPU>"];
tokens = extractBetween(txt, "<", ">")Expected output:
tokens = [<missing>, "GPU"]Observe an unmatched marker
value = extractBetween("abc", "x", "c")Expected output:
value = ""Using extractBetween with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how extractBetween changes the result.
Run a small extractBetween example, explain the result, then change one input and compare the output.
FAQ
Which argument types does extractBetween accept?⌄
The documented text input is a string array, character vector, or cell array of character vectors. Both boundaries are text/pattern markers or both are numeric positions; all eight integer classes are accepted as exact position controls. RunMat mode also admits row-wise character matrices and broader text cells.
Can the start and end arguments mix text and numeric positions?⌄
No. Both boundaries must use the same domain.
What happens when a boundary is not found?⌄
The current implementation returns empty extracted text. This behavior is recorded as RunMat behavior rather than independently verified MATLAB parity.
How does Boundaries inclusive work with numeric positions?⌄
Inclusive mode includes both indexed characters. Exclusive mode returns only the characters strictly between them.
Does extractBetween support implicit expansion?⌄
MATLAB-compatible mode permits scalar or same-size boundaries. General compatible broadcasting is a gated RunMat extension and cannot expand character or cell text output.
Are GPU inputs supported?⌄
There is no GPU text kernel. RunMat mode may gather a resident numeric position control; resident numeric text is rejected without provider access.
Related Strings functions
Transform
append · deblank · erase · eraseBetween · erasePunctuation · eraseURLs · extractAfter · extractBefore · insertAfter · insertBefore · join · lower · pad · replace · replaceBetween · reverse · split · splitlines · strcat · strip · strjoin · strjust · strrep · strsplit · strtrim · upper
Text Analytics
addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · bagOfWords · cosineSimilarity · doc2sequence · encode · extractFileText · extractHTMLText · fastTextWordEmbedding · findElement · getAttribute · htmlTree · ind2word · isVocabularyWord · normalizeWords · readWordEmbedding · removeLongWords · removeShortWords · removeStopWords · removeWords · stopWords · tokenDetails · tokenizedDocument · trainWordEmbedding · vaderSentimentScores · vec2word · word2ind · word2vec · wordEncoding · writeWordEmbedding
Core
blanks · char · compose · convertCharsToStrings · convertContainedStringsToChars · convertStringsToChars · genvarname · int2str · isletter · isspace · isStringScalar · isstrprop · mat2str · native2unicode · newline · num2str · sprintf · sscanf · str2double · str2num · strcmp · strcmpi · string · string.empty · strings · strlength · strncmp · strncmpi · strtok · unicode2native
Search
contains · endsWith · matches · startsWith · strfind
Pattern
digitsPattern · lettersPattern · pattern · regexpPattern · textBoundary · wildcardPattern
Source you can read
Every RunMat function's source is public. Read exactly how extractBetween is executed, line by line, in Rust.
- View the source for extractBetween in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.