extractBetween
Extract substrings between start and end points
Syntax
Description
extracts the substring fromnewStr
= extractBetween(str
,startStr
,endStr
)str
that occurs between the substringsstartStr
andendStr
. The extracted substring does not includestartStr
andendStr
.
newStr
是一个字符串array ifstr
是一个字符串array. Otherwise,newStr
is a cell array of character vectors.
Ifstr
是一个字符串array or a cell array of character vectors, thenextractBetween
extracts substrings from each element ofstr
.
forces the starts and ends specified in any of the previous syntaxes to be either inclusive or exclusive. They are inclusive whennewStr
= extractBetween(___,'Boundaries',bounds)bounds
is'inclusive'
, and exclusive whenbounds
is'exclusive'
. For example,extractBetween(str,startStr,endStr,'Boundaries','inclusive')
returnsstartStr
,endStr
, and all the text between them asnewStr
.
Examples
Select Text Between Substrings
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"
. TheextractBetween
function 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 with Inclusive and Exclusive Boundaries
Select text from string arrays with boundaries that are forced to be inclusive or exclusive.extractBetween
includes the boundaries with the selected text when the boundaries are inclusive.extractBetween
does 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"
Select Text Between Positions in Character Vector
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
str
—Input text
string array|character vector|cell array of character vectors
Input text, specified as a string array, a character vector, or a cell array of character vectors.
Data Types:string
|char
|cell
startStr
—String that indicates start of substring to extract
string array|character vector|cell array of character vectors
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, thenstartStr
can 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 betweenAB
andYZ
in each element ofstr
.
Example:Ifstr
is a2
-by-1
string array, thenextractBetween(str,["AB";"FG"],["YZ";"ST"])
extracts the substrings betweenAB
andYZ
instr(1)
, and betweenFG
andST
instr(2)
.
Data Types:string
|char
|cell
endStr
—String that indicates end of substring to extract
string array|character vector|cell array of character vectors
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, thenendStr
can 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 betweenAB
andYZ
in each element ofstr
.
Example:Ifstr
is a2
-by-1
string array, thenextractBetween(str,["AB";"FG"],["YZ";"ST"])
extracts the substrings betweenAB
andYZ
instr(1)
, and betweenFG
andST
instr(2)
.
Data Types:string
|char
|cell
startPos
—Start position of substring to extract
numeric array
Start position of substring to extract, specified as a numeric array.
Ifstr
is an array with multiple pieces of text, thenstartPos
can 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:Ifstr
is a2
-by-1
string 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
endPos
—End position of substring to extract
numeric array
End position of substring to extract, specified as a numeric array.
Ifstr
is an array with multiple pieces of text, thenendPos
can 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:Ifstr
is a2
-by-1
string 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
newStr
— Output text
string array | cell array of character vectors
Output text, returned as a string array or a cell array of character vectors.
Data Types:string
|cell
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function supports tall arrays with the limitation:
Expansion in the first dimension is not supported with tall arrays.
For more information, seeTall Arrays for Out-of-Memory Data.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
See Also
count
|erase
|eraseBetween
|extractAfter
|extractBefore
|insertAfter
|insertBefore
|join
|replace
|replaceBetween
|size
|split
|strlength
Introduced in R2016b
Open Example
A modified version of this example exists on your system. Do you want to open this version instead?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)