matches

Determine if pattern matches strings

Description

example

TF = matches(str,pattern) returns 1 (true) if the specified pattern matches str, and returns 0 (false) otherwise.

If pattern is an array containing multiple patterns, then matches returns 1 if it finds that any element of pattern matches str.

example

TF = matches(str,pattern,'IgnoreCase',true) ignores case when determining if pattern matches str.

Examples

collapse all

Create a string array.

str = ["Mercury","Venus","Earth","Mars"]
str = 1x4 string
    "Mercury"    "Venus"    "Earth"    "Mars"

Find the strings that match "Earth". Return a logical array where the position of each element equal to 1 corresponds to the position of a matching string in str.

TF = matches(str,"Earth")
TF = 1x4 logical array

   0   0   1   0

Display the match by indexing back into str using TF.

str(TF)
ans = 
"Earth"

Create a string array.

str = ["Mercury","Venus","Earth","Mars"]
str = 1x4 string
    "Mercury"    "Venus"    "Earth"    "Mars"

Find elements of str that match either "Venus" or "Earth".

TF = matches(str,["Venus","Earth"])
TF = 1x4 logical array

   0   1   1   0

Display the matches by indexing into str using TF.

str(TF)
ans = 1x2 string
    "Venus"    "Earth"

Create a string array.

str = ["Mercury","Venus","Earth","Mars"]
str = 1x4 string
    "Mercury"    "Venus"    "Earth"    "Mars"

Find the element of str that matches "earth", ignoring case.

TF = matches(str,"earth","IgnoreCase",true)
TF = 1x4 logical array

   0   0   1   0

Display the matching string.

str(TF)
ans = 
"Earth"

Input Arguments

collapse all

Input text, specified as a string array, a character vector, or a cell array of character vectors.

Search pattern, specified as a string array, a character vector, or a cell array of character vectors.

Extended Capabilities

Introduced in R2019b