lineBoundary - Match start or end of line - MATLAB (original) (raw)

Main Content

Match start or end of line

Since R2020b

Syntax

Description

[pat](#mw%5Fce65e61d-2185-4623-9074-5f0a2455fc85%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = 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](#mw%5Fce65e61d-2185-4623-9074-5f0a2455fc85%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = lineBoundary([type](#mw%5Fce65e61d-2185-4623-9074-5f0a2455fc85%5Fsep%5Fmw%5F3db4ca55-65f2-47cb-817d-7df607c4d0ee)) specifies whether to match at the start or end of a line. type can be'start', 'end', or 'either' (default).

example

Examples

collapse all

Match Boundaries of Lines

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"

Match Start and End Boundaries of Lines

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.

ans = 2x1 string "This is line one...." "Here is line two...."

Negate Boundaries of Lines

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

type — Boundary type

'either' (default) | 'start' | 'end'

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

Data Types: char | string

Output Arguments

collapse all

pat — Pattern expression

pattern object

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