View all functions

CategoryStrings: Regex

What does the regexprep function do in MATLAB / RunMat?

regexprep(str, pattern, replacement) searches str for regular expression matches and replaces each match with the specified replacement text. Inputs can be character vectors, string scalars, string arrays, or cell arrays of text, and the results honour MATLAB's container semantics.

How does the regexprep function behave in MATLAB / RunMat?

  • With scalar text inputs, the result is a scalar of the same kind (char inputs stay char, string scalars stay string scalars).
  • String arrays and cell arrays are processed element-wise and produce containers with matching shapes. When patterns and replacements are provided as arrays of the same size, they are paired element-by-element; otherwise, scalar patterns/replacements broadcast to every element.
  • Cell or string arrays of patterns apply sequentially: each (pattern, replacement) pair is applied in order to every element.
  • The 'once' flag limits each element to the first match. 'emptymatch','remove' (default) skips zero-length matches, while 'emptymatch','allow' lets them participate.
  • 'ignorecase' and 'matchcase' control case sensitivity. 'lineanchors', 'dotall', and 'dotExceptNewline' toggle multiline and dot behaviours in the same way as regexp.
  • 'preservecase' adjusts the replacement text so the case pattern of the first match (all upper, all lower, or title case) is preserved.

regexprep Function GPU Execution Behaviour

regexprep executes entirely on the CPU. When the subject, pattern, or replacement values originate from GPU-resident arrays, RunMat gathers them to host memory before performing the replacements. Results remain on the host; callers that need GPU residency should explicitly move the values back afterwards (e.g. with gpuArray).

Examples of using the regexprep function in MATLAB / RunMat

Replacing vowels in a character vector

clean = regexprep('abracadabra', '[aeiou]', 'X');

Expected output:

clean =
    'XbrXcXdXbrX'

Applying multiple pattern/replacement pairs sequentially

result = regexprep("Color: red", {'Color', 'red'}, {'Shade', 'blue'});

Expected output:

result = "Shade: blue"

Performing case-insensitive replacements

names = regexprep(["Alpha"; "beta"; "GaMmA"], 'a', '_', 'ignorecase');

Expected output:

names =
  3×1 string array
    "_lph_"
    "bet_"
    "G__mM_"

Preserving case of the original match

words = regexprep('MATLAB and matlab', 'matlab', 'runmat', 'preservecase');

Expected output:

words =
    'RUNMAT and runmat'

Limiting replacements to the first match with 'once'

out = regexprep("abababa", 'ba', 'XY', 'once');

Expected output:

out = "aXYbaba"

Using element-wise patterns for a string array

expr = ["foo", "bar"];
pat = ["f", "ar"];
rep = ["F", "AR"];
exact = regexprep(expr, pat, rep);

Expected output:

exact = 1×2 string
    "Foo"    "bAR"

FAQ

How are container outputs shaped?

Outputs mirror the input container. Character vectors return character vectors, string arrays return string arrays with the same size, and cell arrays return cell arrays with matching shape.

Can I supply multiple patterns at once?

Yes. Provide pattern and replacement as equally-sized cell or string arrays. Each pair is applied sequentially to every element, mirroring MATLAB's behaviour.

What if my replacement needs capture-group text?

Use $1, $2, … for numbered groups or ${name} for named groups inside the replacement text. These tokens expand to the corresponding captured substrings.

Does regexprep support case-insensitive matching?

Yes. Pass the 'ignorecase' option (or 'matchcase' to revert). You can also request multiline anchors or dot behaviour changes with 'lineanchors', 'dotall', or 'dotExceptNewline'.

What does 'preservecase' do?

When enabled, regexprep inspects the alphabetic characters in the matched text. If they are all uppercase, lowercase, or title-cased, the replacement text is adjusted to match that style.

Does regexprep execute on the GPU?

No. All matching and replacement runs on the CPU. GPU inputs are downloaded automatically, but the results remain in host memory.

See Also

regexp, regexpi, strrep, replace, contains

Source & Feedback