str_extract {stringr}R Documentation

Extract first piece of a string that matches a pattern.

Description

Vectorised over string. pattern should be a single pattern, i.e. a character vector of length one.

Usage

  str_extract(string, pattern)

Arguments

string

input vector. This must be an atomic vector, and will be coerced to a character vector

pattern

pattern to look for, as defined by a POSIX regular expression. See the “Extended Regular Expressions” section of regex for details. See fixed, ignore.case and perl for how to use other types of matching: fixed, case insensitive and perl-compatible.

Value

character vector.

See Also

str_extract_all to extract all matches

Examples

shopping_list <- c("apples x4", "flour", "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")

[Package stringr version 0.6.2 Index]