textBoundary - Match start or end of text - MATLAB (original) (raw)
Main Content
Match start or end of text
Since R2020b
Syntax
Description
[pat](#mw%5F31a2088b-3c95-4798-b029-c2952b35b0f6%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = 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.
[pat](#mw%5F31a2088b-3c95-4798-b029-c2952b35b0f6%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = textBoundary([type](#mw%5F31a2088b-3c95-4798-b029-c2952b35b0f6%5Fsep%5Fmw%5F3db4ca55-65f2-47cb-817d-7df607c4d0ee))
specifies whether to match at the start or end of text. type
can be'start'
, 'end'
, or 'either'
(default).
Examples
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 = 3×1 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 = 3×1 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 = 3×4 string "his" "text" "is" "firs" "ere" "is" "the" "secon" "ow" "there" "are" "thre"
Input Arguments
Boundary type, specified as 'start'
, 'end'
, or 'either'
.
Data Types: char
| string
Extended Capabilities
Version History
Introduced in R2020b