Main Content

lineBoundary

Match start or end of line

Since R2020b

Description

pat = lineBoundary creates a pattern that matches the start or end of a line, including newline characters. lineBoundary can be negated using the ~ operator. When negated, ~lineBoundary matches between any two characters so long as neither is a newline character.

example

pat = lineBoundary(type) specifies whether to match at the start or end of a line. type can be 'start', 'end', or 'either' (default).

example

Examples

collapse all

Use lineBoundary to match the start or end of a line of text.

Create a string with a newline character. Create a pattern that matches letters following the start of a new line.

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

Extract the pattern.

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

Use the "start" option for lineBoundary to match the specified endpoint of a line.

Create a string with newline characters. Create a pattern that matches any characters between two "start" boundaries of lines.

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

Extract the pattern.

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

Use the ~ operator to negate lineBoundary. This matches boundaries between two characters when neither is a newline character.

Create a string with a newline character. Create a pattern that matches letters that are neither at the start nor end of a line of text.

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

Extract the pattern.

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

Input Arguments

collapse all

Boundary type, specified as 'start', 'end', or 'either'.

Data Types: char | string

Output Arguments

collapse all

Pattern expression, returned as a pattern object.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2020b