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 对象形式返回。

相关推荐
LawrenceLan5 小时前
Flutter 零基础入门(十一):空安全(Null Safety)基础
开发语言·flutter·dart
txinyu的博客5 小时前
解析业务层的key冲突问题
开发语言·c++·分布式
码不停蹄Zzz6 小时前
C语言第1章
c语言·开发语言
行者966 小时前
Flutter跨平台开发在OpenHarmony上的评分组件实现与优化
开发语言·flutter·harmonyos·鸿蒙
阿蒙Amon6 小时前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
SmartRadio7 小时前
ESP32添加修改蓝牙名称和获取蓝牙连接状态的AT命令-完整UART BLE服务功能后的完整`main.c`代码
c语言·开发语言·c++·esp32·ble
且去填词7 小时前
Go 语言的“反叛”——为什么少即是多?
开发语言·后端·面试·go
知乎的哥廷根数学学派7 小时前
基于生成对抗U-Net混合架构的隧道衬砌缺陷地质雷达数据智能反演与成像方法(以模拟信号为例,Pytorch)
开发语言·人工智能·pytorch·python·深度学习·机器学习
yeziyfx8 小时前
kotlin中 ?:的用法
android·开发语言·kotlin
charlie1145141918 小时前
嵌入式的现代C++教程——constexpr与设计技巧
开发语言·c++·笔记·单片机·学习·算法·嵌入式