Main Content

搜索和替换文本

处理文本数据通常涉及查找和替换子字符串。有几个函数可用于查找文本并返回不同信息:一些函数确认文本存在,而另一些函数计算出现次数、查找起始索引或提取子字符串。这些函数处理字符向量和字符串标量(如 "yes")以及字符和字符串数组,如 ["yes","no";"abc","xyz"]。此外,您可以使用模式来定义搜索规则,例如一个或多个字母或数字字符。

搜索文本

要确定文本是否存在,请使用返回逻辑值的函数,如 containsstartsWithendsWith。逻辑值 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

使用提取函数查找和提取文本,如 extractextractBetweenextractBeforeextractAfter

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"

使用函数 replacesongs 中匹配 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"

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

另请参阅

| | | | |

相关主题