Main Content

matches

Determine if pattern matches strings

Description

example

TF = matches(str,pat)returns1(true) if the specified pattern matchesstr, and returns0(false) otherwise.

Ifpatis an array containing multiple patterns, thenmatchesreturns1if it finds that any element ofpatmatchesstr.

example

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

Examples

collapse all

Create a string array.

str = ["Mercury","Venus",“地球”,"Mars"]
str =1x4 string"Mercury" "Venus" "Earth" "Mars"

Find the strings that match“地球”. Return a logical array where the position of each element equal to1corresponds to the position of a matching string instr.

TF = matches(str,“地球”)
TF =1x4 logical array0 0 1 0

Display the match by indexing back intostrusingTF.

str(TF)
ans = "Earth"

Since R2020b

Create a string array that represents numbers. Some of the numbers are hexadecimal numbers with the0xprefix.

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 letterA-F, or any lowercase lettera-f. Then, specify a pattern that begins with0xand is followed by any number of hexadecimal digits.

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

Find the elements ofstrthat match. (The last element does not match because it contains an error:Gis not a hexadecimal digit.)

TF = matches(str,pat)
TF =1x5 logical array0 1 1 0 0

To display the matches, index intostrusingTF.

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

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

For more information on hexadecimal numbers, see十六进制的d Binary Values.

Create a string array.

str = ["Mercury","Venus",“地球”,"Mars"]
str =1x4 string"Mercury" "Venus" "Earth" "Mars"

Find elements ofstrthat match either"Venus"or“地球”.

TF = matches(str,["Venus",“地球”])
TF =1x4 logical array0 1 1 0

Display the matches by indexing intostrusingTF.

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

Create a string array.

str = ["Mercury","Venus",“地球”,"Mars"]
str =1x4 string"Mercury" "Venus" "Earth" "Mars"

Find the element ofstrthat matches"earth", ignoring case.

TF = matches(str,"earth","IgnoreCase",真正的)
TF =1x4 logical array0 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

  • patternarray(since R2020b)

Extended Capabilities

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

Version History

Introduced in R2019b