namedPattern

Labels a pattern with a name so the text it matches can be identified.

Category: Language Fundamentals > Data Types

Syntax

p = namedPattern(pat, name)

Returns pat tagged with the identifier name.

Description

The name does not change what the pattern matches. It is stored in the pattern and shown when the pattern is displayed, which makes long compound patterns easier to read and debug.

The name must follow the rules for variable names: letters, digits and underscores, starting with a letter. Anything else raises an error.

Input arguments

pat
A pattern, or text that is converted to a pattern.
name
Text that is a valid MATLAB identifier.

Output arguments

p
A pattern that matches exactly what pat matches.

Example

np = namedPattern(digitsPattern, "num")
extract("ab12cd", "b" + np)
Output
np = pattern
  Matching num:

    digitsPattern
ans = "b12"

See also

pattern digitsPattern extract contains regexpPattern