How to Use str_extract in R (With Examples)

Spread the love

The str_extract function in R is a useful function from the stringr package that allows you to extract matched patterns defined by regular expressions from a string. Regular expressions or regex are sequences of characters that define a search pattern. They are highly useful for string manipulation, and str_extract is one of the many functions in R that incorporate them.

Basic Usage of str_extract

The basic syntax of the str_extract function is as follows:

str_extract(string, pattern)

Here, string is the input string from which we want to extract a substring, and pattern is the regular expression defining the pattern to be matched.



string <- "The Quick Brown Fox"
pattern <- "[A-Z][a-z]+"
str_extract(string, pattern)

This will extract the first word that starts with an uppercase letter followed by one or more lowercase letters and will return "The".

Examples of Using str_extract

Let’s delve into a series of examples demonstrating various use cases for the str_extract function in R.

1. Extracting Words

To extract words from a string, you can use the \\w+ pattern. It will match one or more word characters.

string <- "The quick brown fox jumps over the lazy dog"
pattern <- "\\w+"
str_extract(string, pattern)

This will return "The", as it extracts the first match of the pattern.

2. Extracting Numbers

If you want to extract numbers from a string, you can use the \\d+ pattern, which matches one or more digits.

string <- "There are 123 apples and 456 oranges."
pattern <- "\\d+"
str_extract(string, pattern)

This will return "123", extracting the first sequence of digits from the string.

3. Extracting Emails

To extract an email address, you can use a more complex pattern.

string <- "Contact us at for more information."
pattern <- "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"
str_extract(string, pattern)

This will return "" as it matches the first email address in the string.

Extracting Multiple Matches

The str_extract function will only return the first match found. If you want to extract all the matches in a string, you should use the str_extract_all function.


string <- "The quick brown fox jumps over the lazy dog"
pattern <- "\\w+"
str_extract_all(string, pattern)

This will return a list containing all the words in the string: "The" "quick" "brown" "fox" "jumps" "over" "the" "lazy" "dog".

Case Study: Extracting URLs

Let’s say we have a string that contains multiple URLs, and we want to extract all of them. The regular expression pattern for matching URLs can be quite intricate due to the various URL formats available.


string <- "Visit or for more information."
pattern <- "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
str_extract_all(string, pattern)

In this case, the str_extract_all function will return both URLs in the string: "" "".

Handling Vector Inputs

The str_extract and str_extract_all functions can also handle vector inputs.


# Vector of strings
strings <- c("Apple 123", "Banana 456", "Cherry 789")

# Pattern to match numbers
pattern <- "\\d+"

# Use str_extract to extract the first match from each string
str_extract(strings, pattern)

This will return a character vector with the first sequence of digits from each string: "123" "456" "789".

Extracting Groups

Sometimes, you may want to extract only a specific group from the matched pattern. You can use parentheses () to define groups in your regular expression and extract them using the str_match function.


string <- "The price is $45.99."
pattern <- "\\$(\\d+\\.\\d+)"
str_match(string, pattern)[,2]

In this example, the entire pattern \\$(\\d+\\.\\d+) will match $45.99, but using str_match and indexing [,2], we are extracting only the numeric part 45.99.


The str_extract function in R, from the stringr package, provides a versatile and powerful way to extract substrings from strings based on regular expression patterns. Here are the key points to remember:

  1. Install and Load stringr Package: The stringr package must be installed and loaded before using str_extract.
  2. Basic Usage: The str_extract function uses a string and a pattern, where the pattern is defined by a regular expression, to extract the first matching substring.
  3. Multiple Matches: To extract all matches from a string, use the str_extract_all function, which returns a list of all matches.
  4. Vector Inputs: Both str_extract and str_extract_all can handle vector inputs, applying the pattern to each element of the vector.
  5. Extracting Groups: Use str_match to extract specific groups from the matched pattern.
  6. Complex Patterns: Regular expressions can be simple or intricate, depending on the pattern you are trying to match, so understanding regular expressions is crucial for effective use of str_extract.

By mastering the use of str_extract along with regular expressions, you can perform a myriad of string manipulation tasks in R, ranging from simple word extraction to handling more complex patterns like email addresses and URLs.

Posted in RTagged

Leave a Reply