matches
Determine if pattern matches strings
Description
Examples
Match Text
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 to1
corresponds to the position of a matching string instr
.
TF = matches(str,“地球”)
TF =1x4 logical array0 0 1 0
Display the match by indexing back intostr
usingTF
.
str(TF)
ans = "Earth"
Match Hexadecimal Numbers Using Patterns
Since R2020b
Create a string array that represents numbers. Some of the numbers are hexadecimal numbers with the0x
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 letterA
-F
, or any lowercase lettera
-f
. Then, specify a pattern that begins with0x
and 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 ofstr
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 array0 1 1 0 0
To display the matches, index intostr
usingTF
.
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.
Match Multiple Strings
Create a string array.
str = ["Mercury","Venus",“地球”,"Mars"]
str =1x4 string"Mercury" "Venus" "Earth" "Mars"
Find elements ofstr
that match either"Venus"
or“地球”
.
TF = matches(str,["Venus",“地球”])
TF =1x4 logical array0 1 1 0
Display the matches by indexing intostr
usingTF
.
str(TF)
ans =1x2 string"Venus" "Earth"
Ignore Case
Create a string array.
str = ["Mercury","Venus",“地球”,"Mars"]
str =1x4 string"Mercury" "Venus" "Earth" "Mars"
Find the element ofstr
that 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
str
—Input text
string array|character vector|cell array of character vectors
Input text, specified as a string array, character vector, or cell array of character vectors.
pat
—Search pattern
string array|character vector|cell array of character vectors|pattern
array (since R2020b)
Search pattern, specified as one of the following:
String array
Character vector
Cell array of character vectors
pattern
array(since R2020b)
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Usage notes and limitations:
Pattern objects are not supported.
For more information, seeTall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
or accelerate code with Parallel Computing Toolbox™ThreadPool
.
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
pat
must be a string array, character vector, or a cell array of character vectors.
For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
Version History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)