Main Content

textBoundary

匹配文本的开头或结尾

自 R2020b 起

说明

示例

pat = textBoundary 创建与文本开头或结尾匹配的模式。textBoundary 可以使用 ~ 运算符求反。当求反时,textBoundary 匹配除文本开头或结尾以外的每个字符之间的边界。

示例

pat = textBoundary(type) 指定是匹配文本的开头还是结尾。type 可以是 'start''end''either'(默认值)。

示例

全部折叠

使用 textBoundary 匹配文本的开头或结尾。

创建一个包含多段文本的字符串数组。创建一个匹配每段文本的第一个单词的模式。

txts = ["This is the first piece of text" 
    "Here is the second" 
    "Now there are three"];
pat = textBoundary + lettersPattern;

提取该模式。

firstWords = extract(txts,pat)
firstWords = 3x1 string
    "This"
    "Here"
    "Now"

使用 textBoundary"end" 选项匹配一段文本的指定端点。

创建一个包含多段文本的字符串数组。创建一个匹配每段文本的最后一个单词的模式。

txts = ["This is the first piece of text" 
    "Here is the second" 
    "Now there are three"];
pat = lettersPattern + textBoundary("end");

提取该模式。

lastWords = extract(txts,pat)
lastWords = 3x1 string
    "text"
    "second"
    "three"

使用 ~ 运算符对 textBoundary 求反。当两个字符都不是文本的开头或结尾时,该模式匹配这两个字符之间的边界。

创建一个包含多段文本的字符串数组。创建一个模式,该模式匹配一段文本中既不在其开头也不在其结尾的字母。

txts = ["This text is first" 
    "Here is the second" 
    "Now there are three"];
pat = ~textBoundary + lettersPattern + ~textBoundary;

提取该模式。

lastWords = extract(txts,pat)
lastWords = 3x4 string
    "his"    "text"     "is"     "firs" 
    "ere"    "is"       "the"    "secon"
    "ow"     "there"    "are"    "thre" 

输入参数

全部折叠

边界类型,指定为 'start''end''either'

数据类型: char | string

输出参量

全部折叠

模式表达式,以 pattern 对象形式返回。

扩展功能

基于线程的环境
使用 MATLAB® backgroundPool 在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool 加快代码运行速度。

版本历史记录

在 R2020b 中推出