str_extract | R Documentation |
str_extract()
extracts the first complete match from each string,
str_extract_all()
extracts all matches from each string.
str_extract(string, pattern, group = NULL)
str_extract_all(string, pattern, simplify = FALSE)
string |
Input vector. Either a character vector, or something coercible to one. |
pattern |
Pattern to look for. The default interpretation is a regular expression, as described in
Match a fixed string (i.e. by comparing only bytes), using
Match character, word, line and sentence boundaries with
|
group |
If supplied, instead of returning the complete match, will return the matched text from the specified capturing group. |
simplify |
A boolean.
|
str_extract()
: an character vector the same length as string
/pattern
.
str_extract_all()
: a list of character vectors the same length as
string
/pattern
.
str_match()
to extract matched groups;
stringi::stri_extract()
for the underlying implementation.
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")
str_extract(shopping_list, "([a-z]+) of ([a-z]+)")
str_extract(shopping_list, "([a-z]+) of ([a-z]+)", group = 1)
str_extract(shopping_list, "([a-z]+) of ([a-z]+)", group = 2)
# 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"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.