Main Content

textBoundary

Match start or end of text

Since R2020b

Description

pat = textBoundary creates a pattern that matches the start or end of text. textBoundary can be negated using the ~ operator. When negated, textBoundary matches between every character except at the start or end of the text.

example

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

example

Examples

collapse all

Use textBoundary to match the start or end of text.

Create a string array with multiple pieces of text. Create a pattern that matches the first word of each piece of text.

txts = ["This is the first piece of text" 
    "Here is the second" 
    "Now there are three"];
pat = textBoundary + lettersPattern;

Extract the pattern.

firstWords = extract(txts,pat)
firstWords = 3x1 string
    "This"
    "Here"
    "Now"

Use the "end" option for textBoundary to match the specified endpoint of a piece of text.

Create a string array with multiple pieces of text. Create a pattern that matches the last word of each piece of text.

txts = ["This is the first piece of text" 
    "Here is the second" 
    "Now there are three"];
pat = lettersPattern + textBoundary("end");

Extract the pattern.

lastWords = extract(txts,pat)
lastWords = 3x1 string
    "text"
    "second"
    "three"

Use the ~ operator to negate textBoundary. This matches boundaries between two characters when neither is the start or end of text.

Create a string array with multiple pieces of text. Create a pattern that matches letters that are neither at the start or end of a piece of text.

txts = ["This text is first" 
    "Here is the second" 
    "Now there are three"];
pat = ~textBoundary + lettersPattern + ~textBoundary;

Extract the pattern.

lastWords = extract(txts,pat)
lastWords = 3x4 string
    "his"    "text"     "is"     "firs" 
    "ere"    "is"       "the"    "secon"
    "ow"     "there"    "are"    "thre" 

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