Method Reference: string.extract

string: newstr = extract (str, pat)
string: newstr = extract (str, pos)

Extract substrings from a string array.

newstr = extract (str, pat) returns the substrings of str that match pat. pat can be a string array, a character vector, or a cell array of character vectors; when it contains more than one piece of text, any of them may match. Within each element of str the matches are found left to right and do not overlap; where alternatives match at the same position, the first listed in pat is taken.

The matches of an element occupy a row of newstr, so the matches run along the second dimension and every element of str must yield the same number of matches. For a string scalar with n matches, newstr is 1xn; for a non-scalar str, newstr has one row per element (taken in column-major order) and one column per match. Elements with no match, including missing values, are treated as having zero matches.

newstr = extract (str, pos) returns the single character located at position pos in each element of str. pos must be a positive integer that is either a scalar, applied to every element, or the same size as str, applied element-wise. In this syntax newstr has the same size as str and missing values are preserved.

Source Code: string

Example: 1

extract pulls out every occurrence of a pattern, returning the matched pieces as a string array.

 s = string ('cat dog cat bird cat')
s =
  string

   "cat dog cat bird cat"
 extract (s, 'cat')
ans =
  1x3 string array

    "cat"    "cat"    "cat"