Patterns to search and match text
A pattern defines rules for matching text with text-searching
functions like contains
, matches
, and
extract
. You can build a pattern expression using
pattern functions, operators, and literal text. For example, MATLAB® release names, start with "R"
, followed by the four-digit
year, and then either "a"
or "b"
. Define a pattern to
match the format of the release names:
pat = "R" + digitsPattern(4) + ("a"|"b");
Match that pattern in a string:
str = ["String was introduced in R2016b." "Pattern was added in R2020b."]; extract(str,pat)
ans = 2x1 string array "R2016b" "R2020b"
Patterns are composed of literal text and other patterns using the +
,
|
, and ~
operators. You also can create common
patterns using Object Functions, which use rules often
associated with regular expressions:
Character-Matching Patterns – Ranges of letters or
digits, wildcards, or whitespaces, such as lettersPattern
.
Search Rules – How many times the pattern must occur,
case sensitivity, optional patterns, and named expressions, such as optionalPattern
.
Boundaries – Boundaries at the start or end of a run
of specific characters, such as alphanumericBoundary
. Boundary patterns can
be negated using the ~
operator so that matches to the boundary
prevents matching of their pattern expression.
Pattern Organization – Define pattern structure and
specify how pattern expressions are displayed, such as maskedPattern
and namedPattern
.
The function pattern
also creates pattern functions with the syntax,
pat = pattern(txt)
, where txt
is literal text that
pat
matches. Pattern functions are useful for specifying pattern type for
function argument validation. However, the pattern
function is rarely
needed for other cases because MATLAB text-matching functions accept text inputs.