Main Content

matches

Determine if pattern matches strings

Description

TF = matches(str,pat) returns 1 (true) if the specified pattern matches str, and returns 0 (false) otherwise. If str is an array, then TF is an array of the same size.

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

example

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

example

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"

Since R2020b

Create a string array that represents numbers. Some of the numbers are hexadecimal numbers with the 0x prefix.

str = ["137","0xA7B","0x1248","72","0xG7"]
str = 1x5 string
    "137"    "0xA7B"    "0x1248"    "72"    "0xG7"

Create a pattern that matches the hexadecimal numbers. To match a single hexadecimal digit, specify a pattern that matches any digit, any capital letter A-F, or any lowercase letter a-f. Then, specify a pattern that begins with 0x and is followed by any number of hexadecimal digits.

pat = digitsPattern(1) | characterListPattern("A","F") | characterListPattern("a","f");
pat = "0x" + asManyOfPattern(pat)
pat = pattern
  Matching:

    "0x" + asManyOfPattern(digitsPattern(1) | characterListPattern("A","F") | characterListPattern("a","f"))

Find the elements of str that match. (The last element does not match because it contains an error: G is not a hexadecimal digit.)

TF = matches(str,pat)
TF = 1x5 logical array

   0   1   1   0   0

To display the matches, index into str using TF.

str(TF)
ans = 1x2 string
    "0xA7B"    "0x1248"

For a list of functions that create pattern objects, see pattern.

For more information on hexadecimal numbers, see Hexadecimal and Binary Values.

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, character vector, or cell array of character vectors.

Search pattern, specified as one of the following:

  • String array

  • Character vector

  • Cell array of character vectors

  • pattern array (since R2020b)

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2019b