目录
lineBoundary函数的功能是匹配行首或行尾。
语法
cpp
pat = lineBoundary
pat = lineBoundary(type)
说明
pat = lineBoundary 创建与一行的行首或行尾(包括 newline 字符)匹配的模式。lineBoundary 可以使用 ~ 运算符求反。当求反时,~lineBoundary 匹配任意两个字符之间的边界,但这两个字符都不能是 newline 字符。
**pat = lineBoundary(type)**指定是匹配行首还是行尾。type 可以是 'start'、'end' 或 'either'(默认值)。
示例
匹配行的边界
使用 lineBoundary 匹配一行文本的开头或结尾。创建一个包含 newline 字符的字符串。创建一个模式,它匹配新行开头后的字母。
cs
txt = "This is line one." + newline + "Here is line two.";
pat = lineBoundary + lettersPattern;
提取该模式。
cs
firstWord = extract(txt,pat)
firstWord = 2x1 string
"This"
"Here"
匹配行的开头和结尾边界
使用 lineBoundary 的 "start" 选项来匹配行的指定端点。创建一个包含 newline 字符的字符串。创建一个模式,它匹配行的两个 "start" 边界之间的任何字符。
cpp
txt = "This is line one." + newline + "Here is line two." + newline + "Last but not least.";
pat = lineBoundary("start") + wildcardPattern(1,inf) + lineBoundary("start");
提取该模式。
cpp
extract(txt,pat)
ans = 2x1 string
"This is line one...."
"Here is line two...."
对行的边界求反
使用 ~ 运算符对 lineBoundary 求反。当两个字符都不是 newline 字符时,该模式匹配这两个字符之间的边界。
创建一个包含 newline 字符的字符串。创建一个匹配字母的模式,这些字母既不在一行文本的开头也不在末尾。
cpp
txt = "This is line one" + newline + "Here is line two";
pat = ~lineBoundary + lettersPattern + ~lineBoundary;
提取该模式。
cs
firstWord = extract(txt,pat)
firstWord = 8x1 string
"his"
"is"
"line"
"on"
"ere"
"is"
"line"
"tw"
参数说明
type --- 边界类型
边界类型,指定为 'start'、'end' 或 'either'。
pat --- 模式表达式
模式表达式,以 pattern 对象形式返回。