Main Content

lineBoundary

匹配行首或行尾

自 R2020b 起

说明

示例

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"

输入参数

全部折叠

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

数据类型: char | string

输出参量

全部折叠

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

扩展功能

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

版本历史记录

在 R2020b 中推出