optionalPattern

Pattern that matches the given pattern zero or one time.

Category: Language Fundamentals > Data Types

Syntax

p = optionalPattern(pat)

Matches pat if it is present, and the empty text otherwise.

Description

The pattern is greedy: where pat could match, it does. It is the pattern-object counterpart of the ? quantifier in a regular expression. Combine it with other patterns using +, for instance to accept an optional sign before digits.

Input arguments

pat
A pattern or text.

Output arguments

p
A pattern object.

Example

p = "id" + optionalPattern("-") + digitsPattern;
matches(["id12", "id-7", "id"], p)
Output
ans = 1×3 logical array

   1   1   0

See also

pattern digitsPattern possessivePattern matches asManyOfPattern