extract

Extracts the parts of text that match a pattern or a literal string.

Category: Language Fundamentals > Data Types

Syntax

newStr = extract(str, pat)

Returns every match of pat found in str.

Description

extract is a pattern-based search that returns the matched text instead of positions. It is a convenient replacement for regexp(str, expr, 'match') when a pattern object can describe what you want.

The result is always a string array, even when str is a character vector.

Input arguments

str
String array, character vector or cell array of character vectors.
pat
Text to find, or a pattern object such as digitsPattern or lettersPattern.

Output arguments

newStr
String array with one element per match, in order. For several input strings the matches are collected in a column.

Example

Numbers inside text

extract("a1b22c333", digitsPattern)
extract("Item 12 and 34", digitsPattern)
Output
ans = 3×1 string array

    "1"
    "22"
    "333"
ans = 2×1 string array

    "12"
    "34"

See also

extractBetween extractAfter extractBefore regexp digitsPattern strfind