extractBefore - Extract substrings before specified positions - MATLAB (original) (raw)

Extract substrings before specified positions

Syntax

Description

[newStr](#bvdto7k-1-newStr) = extractBefore([str](#bvdto7k-1%5Fsep%5Fbu4l86d-str),[pat](#bvdto7k-1-endStr)) extracts the substring that begins with the first character ofstr and ends before the substring specified bypat. If pat occurs multiple times instr, then newStr is str from the start of str up to the first occurrence ofpat.

If str is a string array or a cell array of character vectors, then extractBefore extracts substrings from each element ofstr. The output argument newStr has the same data type as str.

example

[newStr](#bvdto7k-1-newStr) = extractBefore([str](#bvdto7k-1%5Fsep%5Fbu4l86d-str),[pos](#bvdto7k-1-endPos)) extracts the substring that begins with the first character ofstr and ends before the position specified bypos.

example

Examples

collapse all

Select Text Before Substring

Create string arrays and select text that occurs before substrings.

str = "The quick brown fox"

str = "The quick brown fox"

Extract the substring that occurs before the substring " brown". The extractBefore function selects the text but does not include " brown" in the output.

newStr = extractBefore(str," brown")

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 str.

str = ["The quick brown fox jumps";"over the lazy dog"]

str = 2x1 string "The quick brown fox jumps" "over the lazy dog"

newStr = extractBefore(str,[" brown";" dog"])

newStr = 2x1 string "The quick" "over the lazy"

You also can specify one substring as a position that is applied to all elements of the input string array.

Extract Paths Before File Names Using Patterns

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 = 3x1 string "C:\Temp\MyReport.docx" "C:\Data\Experiment1\Trial1\Sample1.csv" "C:\Temp\Slides.pptx"

To extract the paths, first create a pattern that matches the file name at the end of a path, and then extract the path that comes before 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 any characters except the "\" character.

name = wildcardPattern("Except","")

name = pattern Matching:

wildcardPattern("Except","\")

Then, create a pattern that matches any name found between a "\" character and the end of a string. Use the textBoundary function to match the end of a string.

pat = "" + name + textBoundary

pat = pattern Matching:

"\" + wildcardPattern("Except","\") + textBoundary

Finally, call extractBefore.

paths = extractBefore(str,pat)

paths = 3x1 string "C:\Temp" "C:\Data\Experiment1\Trial1" "C:\Temp"

For a list of functions that create pattern objects, see pattern.

Select Substrings Before Position

Create strings before specified positions.

Select the substring before the sixth character.

newStr = extractBefore(str,6)

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 = 2x1 string "Edgar Allen Poe" "Louisa May Alcott"

newStr = extractBefore(str,[6;7])

newStr = 2x1 string "Edgar" "Louisa"

Select substrings from each element and specify the same position.

newStr = extractBefore(str,12)

newStr = 2x1 string "Edgar Allen" "Louisa May "

Select Text Before Position in Character Vector

Create a character vector. Then create new character vectors that are substrings of chr.

chr = 'peppers and onions'

chr = 'peppers and onions'

Select the substring before the eighth position.

newChr = extractBefore(chr,8)

Select text before a substring.

newChr = extractBefore(chr,' and')

Input Arguments

collapse all

str — Input text

string array | character vector | cell array of character vectors

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

pat — Text or pattern that marks end position

string array | character vector | cell array of character vectors | pattern array

Text or pattern in str that marks the end position for extracted text, specified as one of the following:

The extractBefore 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 end or have different ends in each element ofstr.

pos — End position

numeric array

End position, specified as a numeric array.

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

collapse all

newStr — Output text

string array | character vector | cell array of character vectors

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

Extended Capabilities

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

TheextractBefore function supports tall arrays with the following usage notes and limitations:

For more information, see Tall Arrays.

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

Usage notes and limitations:

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced in R2016b