StrExtract() function from the DescTools package in R is “used to extract a part of a string, defined as a regular expression”.
Syntax
StrExtract(x, pattern)
Parameters
- x: It is a character vector where matches are sought, or an object can be coerced by as.character to a character vector.
- pattern: It is a character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector.
Return value
It returns a character vector.
Example 1: R program of StrExtract() function
library(DescTools)
state <- "We are Venom"
StrExtract(state, "Venom")
Output
[1] "Venom"
Example 2: Using regex in StrExtract() Function
You can also use regular expressions to specify more complex patterns.
library(DescTools)
state <- "We are Venom"
StrExtract(state, "\\bV\\w*m\\b")
Output
[1] "Venom"
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.