str_extract

0th

Percentile

Extract first piece of a string that matches a pattern.

Extract first piece of a string that matches a pattern.

Keywords
character
Usage
str_extract(string, pattern)
Arguments
string
input character vector
pattern
pattern to look for, as defined by a POSIX regular expression. See the ``Extended Regular Expressions'' section of regex for details.
Details

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

Value

  • character vector.

See Also

str_extract_all to extract all matches

Aliases
  • str_extract
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")
Documentation reproduced from package stringr, version 0.5, License: GPL-2

Community examples

shiqi.3.zhang@uconn.edu at Jan 9, 2019 stringr v1.3.1

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")

5864004@yahoo.com.tw at Oct 18, 2018 stringr v1.3.1

# NOT RUN { 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") # 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.", boundary("word")) # }