MATLAB中textBoundary函数用法

目录

语法

说明

示例

匹配文本的边界

匹配文本的结尾边界

对文本的边界求反


textBoundary函数的功能是匹配文本的开头或结尾。

语法

cs 复制代码
pat = textBoundary
pat = textBoundary(type)

说明

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

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

示例

匹配文本的边界

使用 textBoundary 匹配文本的开头或结尾。创建一个包含多段文本的字符串数组。创建一个匹配每段文本的第一个单词的模式。

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

提取该模式。

cs 复制代码
firstWords = extract(txts,pat)
firstWords = 3x1 string
    "This"
    "Here"
    "Now"

匹配文本的结尾边界

使用 textBoundary 的 "end" 选项匹配一段文本的指定端点。创建一个包含多段文本的字符串数组。创建一个匹配每段文本的最后一个单词的模式。

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

提取该模式。

cpp 复制代码
lastWords = extract(txts,pat)
lastWords = 3x1 string
    "text"
    "second"
    "three"

对文本的边界求反

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

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

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

提取该模式。

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

参数说明

type --- 边界类型

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

pat --- 模式表达式

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

相关推荐
weixin_4454766834 分钟前
Java并发编程——synchronized的实现原理与应用
java·开发语言·并发·synchronized
yi碗汤园35 分钟前
【超详细】C#自定义工具类-StringHelper
开发语言·前端·unity·c#·游戏引擎
sali-tec39 分钟前
C# 基于halcon的视觉工作流-章49-网面破损
开发语言·图像处理·算法·计算机视觉·c#
YuanlongWang41 分钟前
c# ABP vNext 框架详解及其模块化开发思想介绍
开发语言·c#
张人玉1 小时前
WPF布局控件(界面骨架核心)
开发语言·c#·wpf·布局控件
闲人编程1 小时前
使用MLflow跟踪和管理你的机器学习实验
开发语言·人工智能·python·机器学习·ml·codecapsule
看兵马俑的程序员2 小时前
RAG实现-本地PDF内容加载和切片
开发语言·python·pdf
专注前端30年2 小时前
【JavaScript】reduce 方法的详解与实战
开发语言·前端·javascript
两个人的幸福online2 小时前
php使用腾讯云服务
开发语言·php·腾讯云
无敌最俊朗@2 小时前
C++ STL Deque 高频面试题与答案
开发语言·c++