matches

Tests whether text matches a pattern in its entirety.

Category: Language Fundamentals > Data Types

Syntax

tf = matches(str, pat)

Returns true for each element of str that is exactly equal to pat, or matched completely by it.

tf = matches(str, pat, IgnoreCase=true)

Ignores case when comparing.

Description

Unlike contains, which asks for the pattern to appear anywhere in the text, matches requires that the whole text be matched. So matches('abc12', lettersPattern) is false because of the digits, while contains would be true.

Input arguments

str
String array, character vector, or cell array of character vectors.
pat
Text, array of texts, or a pattern object. If it is an array, the result is true when any element matches.
IgnoreCase
Logical option; false by default.

Output arguments

tf
Logical array with one value per element of str.

Example

Whole-text matching

matches(["apple", "fig"], "fig")
matches('abc', lettersPattern)
matches('abc12', lettersPattern)
matches("Abc", "abc", IgnoreCase=true)
Output
ans = 1×2 logical array

   0   1
ans = logical
   1
ans = logical
   0
ans = logical
   1

See also

contains startsWith endsWith strcmp pattern