MATLAB 帮助中心
确定模式是否与字符串匹配
TF = matches(str,pat)
TF = matches(str,pat,IgnoreCase=true)
TF = matches(str,pat) 会在指定模式与 str 有匹配时返回 1 (true),否则返回 0 (false)。如果 str 是数组,则 TF 是相同大小的数组。
str
pat
1
true
0
false
TF
如果 pat 是包含多个模式的数组,则 matches 在发现 pat 中任一元素与 str 有匹配时将返回 1。
matches
示例
TF = matches(str,pat,IgnoreCase=true) 在确定 pat 是否与 str 有匹配时忽略大小写。
全部折叠
创建一个字符串数组。
str = ["Mercury","Venus","Earth","Mars"]
str = 1×4 string "Mercury" "Venus" "Earth" "Mars"
查找匹配 "Earth" 的字符串。返回一个逻辑数组,其中每个元素 1 的位置对应于 str 中一个匹配字符串的位置。
"Earth"
TF = matches(str,"Earth")
TF = 1×4 logical array 0 0 1 0
使用 TF 再次对 str 进行索引,以显示匹配项。
str(TF)
ans = "Earth"
自 R2020b 开始提供
创建一个表示数字的字符串数组。有些数是具有 0x 前缀的十六进制数。
0x
str = ["137","0xA7B","0x1248","72","0xG7"]
str = 1×5 string "137" "0xA7B" "0x1248" "72" "0xG7"
创建一个匹配十六进制数字的模式。要匹配单个十六进制数字,请指定一个模式,该模式匹配任意数字、任意大写字母 A-F 或任意小写字母 a-f。然后,指定一个模式,该模式以 0x 开头并后跟任意数量的十六进制数字。
A
F
a
f
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"))
查找匹配的 str 元素。(最后一个元素不匹配,因为它包含错误:G 不是一个十六进制数字。)
G
TF = 1×5 logical array 0 1 1 0 0
要显示匹配项,请使用 TF 对 str 进行索引。
ans = 1×2 string "0xA7B" "0x1248"
有关创建模式对象的函数列表,请参阅pattern。
pattern
有关十六进制数的详细信息,请参阅十六进制和二进制值。
查找匹配 "Venus" 或 "Earth" 的 str 元素。
"Venus"
TF = matches(str,["Venus","Earth"])
TF = 1×4 logical array 0 1 1 0
使用 TF 对 str 进行索引,以显示匹配项。
ans = 1×2 string "Venus" "Earth"
查找与 "earth" 匹配的 str 元素,忽略大小写。
"earth"
TF = matches(str,"earth",IgnoreCase=true)
显示匹配的字符串。
输入文本,指定为字符串数组、字符向量或字符向量元胞数组。
搜索模式,指定为下列值之一:
字符串数组
字符向量
字符向量元胞数组
pattern 数组(自 R2020b 开始提供)
全部展开
matches 函数完全支持 tall 数组。有关详细信息,请参阅 tall 数组。
backgroundPool
ThreadPool
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
此函数完全支持分布式数组。有关详细信息,请参阅使用分布式数组运行 MATLAB 函数 (Parallel Computing Toolbox)。
在 R2019b 中推出
contains | endsWith | extract | startsWith | pattern | digitsPattern | asManyOfPattern | characterListPattern | count
contains
endsWith
extract
startsWith
digitsPattern
asManyOfPattern
characterListPattern
count
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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处