pattern

Class of objects that describe text patterns for searching and matching.

Category: Language Fundamentals > Data Types

Syntax

p = pattern(txt)

Creates a pattern that matches the text txt.

p = p1 + p2

Matches p1 followed by p2.

p = p1 | p2

Matches either p1 or p2.

Description

A pattern is a readable alternative to a regular expression. Building blocks such as digitsPattern, lettersPattern, optionalPattern or asManyOfPattern are combined with + (sequence) and | (alternative). Text values on either side of + are converted automatically.

Patterns are accepted by contains, startsWith, endsWith, matches, extract, replace, split, strfind, count and others. Use matches to test a whole string against a pattern.

Input arguments

txt
String or character vector.

Output arguments

p
A pattern object, usable wherever text patterns are accepted.

Example

p = pattern("ab") + digitsPattern
class(p)
matches(["ab7", "ab", "7"], p)
Output
p = pattern
  Matching:

    "ab" + digitsPattern
ans = 'pattern'
ans = 1×3 logical array

   1   0   0

See also

digitsPattern lettersPattern optionalPattern regexpPattern matches contains