What is StrExtractBetween() Function in R (DescTools Package)

The StrExtractBetween() is a convenience function from the DescTools package in R that extracts the parts of a string between the left and right delimiter. For example, you can extract the word “world” from the string “Hello world!”; you can use the StrExtractBetween(“Hello world!”, ” “, “!”) function, which will return “world”.

Syntax

StrExtractBetween(x, left, right, greedy = FALSE)

Parameters

x: It is a character vector where matches are sought, or an object can be coerced by as.character() to a character vector.

left: It is a left character(s) limiting the string to be extracted.

right: It is a right character(s) limiting the string to be extracted.

greedy: It is logical and determines whether the first found match for right should be used (FALSE, default) or the last (TRUE).

: IThe dotsare passed to the internally used function regexpr(), which allows using e.g., Perl-like regular expressions.

Return value

It is a character vector.

Example

You need to install the DescTools package to use the StrExtractBetween() function.

To install the DescTools package, you can use the install.packages() function with the ‘DescTools’ package as an argument.

See the complete code of the StrExtractBetween() function.

library(DescTools)

state <- "We are Venom"

StrExtractBetween(state, "We", "Venom")

Output

[1] " are "

You can see that we extracted a string between the left and right delimiter.

That’s it.

Leave a Comment