Main Content

extract

Extract substrings from strings

Since R2020b

Description

example

newStr = extract(str,pat) returns any substrings in str that match the pattern specified by pat.

If str is a string array or a cell array of character vectors, then the function extracts substrings from each element of str. If pat is an array, then the function matches against multiple patterns.

example

newStr = extract(str,pos) returns the character in str at the position specified by pos.

Examples

collapse all

Create a string array that contains addresses. Each address ends with a US ZIP code.

str = ["73 Beacon St., Boston, MA, 02116";
       "1640 Riverside Dr., Hill Valley, CA, 92530";
       "138 Main St., Cambridge, MA, 02138"]
str = 3x1 string
    "73 Beacon St., Boston, MA, 02116"
    "1640 Riverside Dr., Hill Valley, CA, 92530"
    "138 Main St., Cambridge, MA, 02138"

Create a pattern that matches any sequence of digits.

pat = digitsPattern
pat = pattern
  Matching:

    digitsPattern

Use it to extract all sequences of digits from the addresses.

newStr = extract(str,pat)
newStr = 3x2 string
    "73"      "02116"
    "1640"    "92530"
    "138"     "02138"

The digitsPattern pattern matches street numbers, apartment numbers, and ZIP codes. To match only ZIP codes, create a pattern that matches a sequence of digits at the end of an address.

pat = digitsPattern + textBoundary
pat = pattern
  Matching:

    digitsPattern + textBoundary

Extract the ZIP codes.

newStr = extract(str,pat)
newStr = 3x1 string
    "02116"
    "92530"
    "02138"

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

Create a string.

str = "All's well that ends well"
str = 
"All's well that ends well"

Extract the first character in the string.

extract(str,1)
ans = 
"A"

Extract the last character.

extract(str,strlength(str))
ans = 
"l"

Input Arguments

collapse all

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

Search pattern, specified as one of the following:

  • String array

  • Character vector

  • Cell array of character vectors

  • pattern array

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

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

If str is a string array, then newStr is also a string array. Otherwise, newStr is a cell array of character vectors.

Version History

Introduced in R2020b