extractAfter - Extract substrings after specified positions - MATLAB (original) (raw)
Extract substrings after specified positions
Syntax
Description
[newStr](#bvdoi7q-1-newStr) = extractAfter([str](#bvdoi7q-1%5Fsep%5Fbu4l86d-str),[pat](#bvdoi7q-1-startStr))
extracts the substring that begins after the substring specified bypat
and ends with the last character ofstr
. If pat
occurs multiple times instr
, then newStr
is str
from the first occurrence of pat
to the end.
If str
is a string array or a cell array of character vectors, then extractAfter
extracts substrings from each element ofstr
. The output argument newStr
has the same data type as str
.
[newStr](#bvdoi7q-1-newStr) = extractAfter([str](#bvdoi7q-1%5Fsep%5Fbu4l86d-str),[pos](#bvdoi7q-1-startPos))
extracts the substring that begins after the position specified bypos
and ends with the last character ofstr
.
Examples
Create string arrays and select text that occurs after substrings.
str = "The quick brown fox"
str = "The quick brown fox"
Extract the substring that occurs after the substring "quick "
. The extractAfter
function selects the new text but does not include "quick "
in the output.
newStr = extractAfter(str,"quick ")
Create a new string array from the elements of a string array. When you specify different substrings as positions, they must be contained in a string array or a cell array that is the same size as the input string array.
str = ["The quick brown fox jumps";"over the lazy dog"]
str = 2×1 string "The quick brown fox jumps" "over the lazy dog"
newStr = extractAfter(str,["quick ";"the "])
newStr = 2×1 string "brown fox jumps" "lazy dog"
You also can specify one substring as a position that is applied to all elements of the input string array.
Since R2020b
Create a string array of file names, including full paths.
str = ["C:\Temp\MyReport.docx"; "C:\Data\Experiment1\Trial1\Sample1.csv"; "C:\Temp\Slides.pptx"]
str = 3×1 string "C:\Temp\MyReport.docx" "C:\Data\Experiment1\Trial1\Sample1.csv" "C:\Temp\Slides.pptx"
To extract the file names, first create a pattern that matches a full path, and then extract what comes after that pattern.
A full path can have several levels, each consisting of any text followed by a "\"
character. So start by creating a pattern that matches one level.
level = wildcardPattern + ""
level = pattern Matching:
wildcardPattern + "\"
Then, create a pattern that matches a full path, containing any number of levels.
pat = asManyOfPattern(level)
pat = pattern Matching:
asManyOfPattern(wildcardPattern + "\")
Finally, call extractAfter
.
filenames = extractAfter(str,pat)
filenames = 3×1 string "MyReport.docx" "Sample1.csv" "Slides.pptx"
For a list of functions that create pattern objects, see pattern.
Create strings after specified positions.
Select the substring after the 12th character.
newStr = extractAfter(str,12)
Select substrings from each element of a string array. When you specify different positions with numeric arrays, they must be the same size as the input string array.
str = ["Edgar Allen Poe";"Louisa May Alcott"]
str = 2×1 string "Edgar Allen Poe" "Louisa May Alcott"
newStr = extractAfter(str,[12;11])
newStr = 2×1 string "Poe" "Alcott"
Select substrings from each element and specify the same position.
newStr = extractAfter(str,6)
newStr = 2×1 string "Allen Poe" " May Alcott"
Create a character vector. Then create new character vectors that are substrings of chr
.
chr = 'peppers and onions'
chr = 'peppers and onions'
Select text after the 12th position.
newChr = extractAfter(chr,12)
Select text after a substring.
newChr = extractAfter(chr,'and ')
Input Arguments
Input text, specified as a string array, character vector, or cell array of character vectors.
Text or pattern in str
that marks the start position for extracted text, specified as one of the following:
- String array
- Character vector
- Cell array of character vectors
- pattern array (since R2020b)
The extractAfter
function excludespat
from the substring that is extracted.
If str
is a string array or cell array of character vectors, then you can extract substrings from every element ofstr
. You can specify that the substrings either all have the same start or have different starts in each element ofstr
.
- To specify the same start, specify
pat
as a character vector, string scalar, orpattern
object. - To specify different starts, specify
pat
as a string array, cell array of character vectors, orpattern
array.
Start position, specified as a numeric array.extractAfter
excludes the character atpos
from the extracted substring.
If str
is a string array or cell array of character vectors, then pos
can be a numeric scalar or numeric array of the same size as str
.
Output Arguments
Output text, returned as a string array, character vector, or cell array of character vectors.
Extended Capabilities
TheextractAfter
function supports tall arrays with the following usage notes and limitations:
- If
pat
is an array of pattern objects, the size of the first dimension of the array must be 1.
For more information, see Tall Arrays.
Usage notes and limitations:
str
andpat
must be a string scalar, a character vector, or a cell array containing not more than one character vector.
Version History
Introduced in R2016b