FUNCTION REFERENCE

regexp

Match a regular expression.

Match a regular expression.

Syntax

[startIndex, endIndex] = regexp(str, expression)

Syntax

out = regexp(str, expression, 'match')

Syntax

[tok, mat] = regexp(str, expression, 'tokens', 'match')

Syntax

out = regexp(str, expression, ..., 'once')

Outputs without option strings come in the order start, end, tokenExtents,

match, tokens, names, split. Option strings 'match', 'tokens', 'names',

'start', 'end', 'split' and 'tokenExtents' pick the outputs and their order.

'names' is a 1-by-N struct array, one element per match, with a field per

named token (?<name>...). 'once' returns the first match only, without the

surrounding cell. No match gives 0x0 results. In an alternation, tokens of

the branches the match did not take are left out.

\b is the backspace character; \< and \> match where a word starts and ends.

'ignorecase', 'lineanchors', 'dotexceptnewline', 'freespacing' and

'emptymatch' change how the expression matches.

A cell array str or expression gives a cell array of results of its shape.

\w, \d and \s match ASCII characters only.

Example

regexp('ab12cd345', '\d+', 'match')   % {'12', '345'}

Differences from MATLAB: lookahead, lookbehind, backreferences such as \1,

atomic groups, possessive quantifiers, conditionals and dynamic expressions

are not supported yet and give an error. A lazy quantifier that can match

empty text finds no match: regexp('aaa', 'a*?', 'match') gives {} where

MATLAB gives {'a', 'a', 'a'}.

String inputs are supported with bounded two-dimensional storage.

Example

regexp("a1b2",'[a-z]','match')

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.