str_extract: Extract match from a string using a pattern

View source: R/extract.R

str_extractR Documentation

Extract match from a string using a pattern

Description

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

Usage

str_extract(string, pattern)

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.

Value

Returns a character vector the same length as string. Non-matching elements will be replaced with NA.

Examples

shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2")
str_extract(shopping_list, "\\d")
str_extract(shopping_list, "[a-z]+")
str_extract(shopping_list, "[a-z]{1,4}")
str_extract(shopping_list, "\\b[a-z]{1,4}\\b")


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