MATLAB中lineBoundary函数用法

目录

语法

说明

示例

匹配行的边界

匹配行的开头和结尾边界

对行的边界求反


        lineBoundary函数的功能是匹配行首或行尾。

语法

pat = lineBoundary
pat = lineBoundary(type)

说明

        pat = lineBoundary 创建与一行的行首或行尾(包括 newline 字符)匹配的模式。lineBoundary 可以使用 ~ 运算符求反。当求反时,~lineBoundary 匹配任意两个字符之间的边界,但这两个字符都不能是 newline 字符。

        pat = lineBoundary(type) 指定是匹配行首还是行尾。type 可以是 'start'、'end' 或 'either'(默认值)。

示例

匹配行的边界

        使用 lineBoundary 匹配一行文本的开头或结尾。创建一个包含 newline 字符的字符串。创建一个模式,它匹配新行开头后的字母。

txt = "This is line one." + newline + "Here is line two.";
pat = lineBoundary + lettersPattern;

提取该模式。

firstWord = extract(txt,pat)
firstWord = 2x1 string
    "This"
    "Here"

匹配行的开头和结尾边界

        使用 lineBoundary 的 "start" 选项来匹配行的指定端点。创建一个包含 newline 字符的字符串。创建一个模式,它匹配行的两个 "start" 边界之间的任何字符。

txt = "This is line one." + newline + "Here is line two." + newline + "Last but not least.";
pat = lineBoundary("start") + wildcardPattern(1,inf) + lineBoundary("start");

提取该模式。

extract(txt,pat)
ans = 2x1 string
    "This is line one...."
    "Here is line two...."

对行的边界求反

        使用 ~ 运算符对 lineBoundary 求反。当两个字符都不是 newline 字符时,该模式匹配这两个字符之间的边界。

        创建一个包含 newline 字符的字符串。创建一个匹配字母的模式,这些字母既不在一行文本的开头也不在末尾。

txt = "This is line one" + newline + "Here is line two";
pat = ~lineBoundary + lettersPattern + ~lineBoundary;

        提取该模式。

firstWord = extract(txt,pat)
firstWord = 8x1 string
    "his"
    "is"
    "line"
    "on"
    "ere"
    "is"
    "line"
    "tw"

参数说明

type — 边界类型

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

pat — 模式表达式

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值