str_extract_all: Extract all matches from a string using a pattern

View source: R/extract.R

str_extract_allR Documentation

Extract all matches from a string using a pattern

Description

Vectorised over string, but not pattern which must be a single string (unlike stringr).

Usage

str_extract_all(string, pattern, simplify = FALSE)

Arguments

string

character vector of strings.

pattern

character, a pattern to match. Can be:

  • A Perl-compatible regular expression (default).

  • Wrap with perl(ignore_case = TRUE) to use case-insensitive matching.

  • Wrap with fixed() to use a fixed/literal match.

  • Wrap with regex() to use a POSIX 1003.2 extended regular expression.

  • Wrap with regex(ignore_case = TRUE) to use case-insensitive matching with a POSIX 1003.2 extended regular expression.

simplify

logical, should output list be simplified to a matrix? Default is FALSE.

Value

Returns a list the same length as string, unless simply = TRUE wherein a character matrix is returned with non-matching elements replaced with "".

Examples

shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2")

# Extract all matches
str_extract_all(shopping_list, "[a-z]+")
str_extract_all(shopping_list, "\\b[a-z]+\\b")
str_extract_all(shopping_list, "\\d")

# Simplify results into character matrix
str_extract_all(shopping_list, "\\b[a-z]+\\b", simplify = TRUE)
str_extract_all(shopping_list, "\\d", simplify = TRUE)

# Extract all words
str_extract_all("This is, suprisingly, a sentence.", "\\w+")


csdaw/stringrb documentation built on Aug. 13, 2022, 10:55 p.m.