How to use str_extract in R?

str_extract is a function in the R programming language which is used to extract parts of a string that match a certain pattern. It takes two arguments; the string to extract from and the pattern to match. It returns the part of the string that matches the pattern. This is useful for extracting data from strings in a consistent way.


The str_extract() function from the package in R can be used to extract matched patterns in a string.

This function uses the following syntax:

str_extract(string, pattern)

where:

  • string: Character vector
  • pattern: Pattern to extract

The following examples show how to use this function in practice.

Example 1: Extract One Pattern from String

The following code shows how to extract the string “ther” from a particular string in R:

library(stringr)

#define string
some_string <- "Hey there my name is Doug"

#extract "ther" from string
str_extract(some_string, "ther")

[1] "ther"

The pattern “ther” was successfully extracted from the string.

Note that if we attempt to extract some pattern that doesn’t exist in the string, we’ll simply receive NA as a result:

library(stringr)

#define string
some_string <- "Hey there my name is Doug"

#attempt to extract "apple" from string
str_extract(some_string, "apple")

[1] NA

Since the pattern “apple” did not exist in the string, a value of NA was returned.

Example 2: Extract Numeric Values from String

The following code shows how to use the regex \d+ to extract only the numeric values from a string:

library(stringr)

#define string
some_string <- "There are 350 apples over there"

#extract only numeric values from string
str_extract(some_string, "\d+")

[1] "350"

Example 3: Extract Characters from Vector of Strings

library(stringr)

#define vector of strings
some_strings <- c("4 apples", "3 bananas", "7 oranges")

#extract only characters from each string in vector
str_extract(some_strings, "[a-z]+")

[1] "apples"  "bananas" "oranges"

Notice that only the characters from each string are returned.

The following tutorials explain how to perform other common tasks in R:

x