搜索和替换文本

您所在的位置:网站首页 matlab替换字符串中的字符 搜索和替换文本

搜索和替换文本

2024-02-01 01:53| 来源: 网络整理| 查看: 265

搜索文本

要确定文本是否存在,请使用返回逻辑值的函数,如 contains、startsWith 或 endsWith。逻辑值 1 对应于 true,0 对应于 false。

txt = "she sells seashells by the seashore"; TF = contains(txt,"sea")TF = logical 1

使用 count 函数计算文本出现的次数。

n = count(txt,"sea")n = 2

要定位文本出现的位置,请使用 strfind 函数,该函数返回起始索引。

idx = strfind(txt,"sea")idx = 1×2 11 28

使用提取函数查找和提取文本,如 extract、extractBetween、extractBefore 或 extractAfter。

mid = extractBetween(txt,"sea","shore")mid = "shells by the sea"

也可以包括边界文本。

mid = extractBetween(txt,"sea","shore","Boundaries","inclusive")mid = "seashells by the seashore" 在数组中查找文本

搜索和替换函数也可以在多元素数组中查找文本。例如,在几首歌曲标题中寻找颜色名称。

songs = ["Yellow Submarine"; "Penny Lane"; "Blackbird"]; colors =["Red","Yellow","Blue","Black","White"]; TF = contains(songs,colors)TF = 3x1 logical array 1 0 1

要列出包含颜色名称的歌曲,请使用 TF 逻辑值数组作为原始 songs 数组的索引。这种方法称为逻辑索引。

colorful = songs(TF)colorful = 2x1 string "Yellow Submarine" "Blackbird"

使用函数 replace 将 songs 中匹配 colors 的元素的文本替换为字符串 "Orange"。

replace(songs,colors,"Orange")ans = 3x1 string "Orange Submarine" "Penny Lane" "Orangebird" 匹配模式

自 R2020b 开始提供

除了搜索字面文本,如“sea”或“yellow”,您还可以搜索匹配某模式的文本。有许多预定义的模式,如 digitsPattern,可用于查找数字。

address = "123a Sesame Street, New York, NY 10128"; nums = extract(address,digitsPattern) nums = 2x1 string "123" "10128"

为了提高搜索精确度,您可以组合使用模式。例如,定位以字符“S”开头的单词。使用一个字符串指定“S”字符,并使用 lettersPattern 查找该字符后的其他字母。

pat = "S" + lettersPattern; StartWithS = extract(address,pat) StartWithS = 2x1 string "Sesame" "Street"

有关详细信息,请参阅构建模式表达式。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3