Documentation

extractBetween

Extract substrings between start and end points

Description

example

newStr= extractBetween(str,startStr,endStr)extracts the substring fromstrthat occurs between the substringsstartStrandendStr. The extracted substring does not includestartStrandendStr.

newStr是一个字符串array ifstr是一个字符串array. Otherwise,newStris a cell array of character vectors.

Ifstr是一个字符串array or a cell array of character vectors, thenextractBetweenextracts substrings from each element ofstr.

example

newStr= extractBetween(str,startPos,endPos)extracts the substring fromstrthat occurs between the positionsstartPosandendPos, including the characters at those positions.extractBetweenreturns the substring asnewStr.

example

newStr= extractBetween(___,'Boundaries',bounds)forces the starts and ends specified in any of the previous syntaxes to be either inclusive or exclusive. They are inclusive whenboundsis'inclusive', and exclusive whenboundsis'exclusive'. For example,extractBetween(str,startStr,endStr,'Boundaries','inclusive')returnsstartStr,endStr, and all the text between them asnewStr.

Examples

collapse all

Create string arrays and select text that occurs between substrings.

Starting in R2017a, you can create strings using double quotes.

str ="The quick brown fox"
str = "The quick brown fox"

Select the text that occurs between the substrings"quick "and" fox". TheextractBetweenfunction selects the text but does not include"quick "or" fox"in the output.

newStr = extractBetween(str,"quick "," fox")
newStr = "brown"

Select substrings from each element of a string array. When you specify different substrings as start and end indicators, they must be contained in a string array or a cell array that is the same size asstr.

str = ["The quick brown fox jumps";"over the lazy dog"]
str =2x1 string array"The quick brown fox jumps" "over the lazy dog"
newStr = extractBetween(str,["quick ";"the "],[" fox";" dog"])
newStr =2x1 string array"brown" "lazy"

Create string arrays and select substrings between start and end positions that are specified as numbers.

Starting in R2017a, you can create strings using double quotes.

str ="Edgar Allen Poe"
str = "Edgar Allen Poe"

Select the middle name. Specify the seventh and 11th positions in the string.

newStr = extractBetween(str,7,11)
newStr = "Allen"

Select substrings from each element of a string array. When you specify different start and end positions with numeric arrays, they must be the same size as the input string array.

str = ["Edgar Allen Poe";"Louisa May Alcott"]
str =2x1 string array"Edgar Allen Poe" "Louisa May Alcott"
newStr = extractBetween(str,[7;8],[11;10])
newStr =2x1 string array"Allen" "May"

Select text from string arrays with boundaries that are forced to be inclusive or exclusive.extractBetweenincludes the boundaries with the selected text when the boundaries are inclusive.extractBetweendoes not include the boundaries with the selected text when the boundaries are exclusive.

Create a string array. Starting in R2017a, you can create strings using double quotes.

str1 ="small|medium|large"
str1 = "small|medium|large"

Select the text between sixth and 13th positions, but do not include the characters at those positions.

newStr = extractBetween(str1,6,13,'Boundaries','exclusive')
newStr = "medium"

Select the text between two substrings, and also the substrings themselves.

str2 ="The quick brown fox jumps over the lazy dog"
str2 = "The quick brown fox jumps over the lazy dog"
newStr = extractBetween(str2," brown","jumps",'Boundaries','inclusive')
newStr = " brown fox jumps"

Create a character vector and select text between start and end positions.

chr ='mushrooms, peppers, and onions'
chr = 'mushrooms, peppers, and onions'
newChr = extractBetween(chr,12,18)
newChr =1x1 cell array{'peppers'}

Select text between substrings.

newChr = extractBetween(chr,'mushrooms, ',', and')
newChr =1x1 cell array{'peppers'}

Input Arguments

collapse all

Input text, specified as a string array, a character vector, or a cell array of character vectors.

Data Types:string|char|cell

String that indicates the start of the substring to extract, specified as a string array, a character vector, or a cell array of character vectors.

Ifstr是一个字符串array or cell array of character vectors, thenstartStrcan be a character vector, a string scalar, or a string array or a cell array of the same size asstr.

Example:extractBetween(str,"AB","YZ")extracts the substrings betweenABandYZin each element ofstr.

Example:Ifstris a2-by-1string array, thenextractBetween(str,["AB";"FG"],["YZ";"ST"])extracts the substrings betweenABandYZinstr(1), and betweenFGandSTinstr(2).

Data Types:string|char|cell

String that indicates the end of the substring to extract, specified as a string array, a character vector, or a cell array of character vectors.

Ifstr是一个字符串array or cell array of character vectors, thenendStrcan be a character vector, a string scalar, or a string array or a cell array of the same size asstr.

Example:extractBetween(str,"AB","YZ")extracts the substrings betweenABandYZin each element ofstr.

Example:Ifstris a2-by-1string array, thenextractBetween(str,["AB";"FG"],["YZ";"ST"])extracts the substrings betweenABandYZinstr(1), and betweenFGandSTinstr(2).

Data Types:string|char|cell

Start position of substring to extract, specified as a numeric array.

Ifstris an array with multiple pieces of text, thenstartPoscan be a numeric scalar or a numeric array of the same size asstr.

Example:extractBetween(str,5,9)extracts the substrings from the fifth through the ninth positions in each element ofstr.

Example:Ifstris a2-by-1string array, thenextractBetween(str,[5;10],[9;21])extracts the substring from the fifth through the ninth positions instr(1), and from the 10th through the 21st positions instr(2).

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

End position of substring to extract, specified as a numeric array.

Ifstris an array with multiple pieces of text, thenendPoscan be a numeric scalar or a numeric array of the same size asstr.

Example:extractBetween(str,5,9)extract the substrings from the fifth through the ninth positions in each element ofstr.

Example:Ifstris a2-by-1string array, thenextractBetween(str,[5;10],[9;21])extracts the substrings from the fifth through the ninth positions instr(1), and from the 10th through the 21st positions instr(2).

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Output Arguments

collapse all

Output text, returned as a string array or a cell array of character vectors.

Data Types:string|cell

Extended Capabilities

Introduced in R2016b