Remove pair(s) of characters from a string. The pair(s) to be removed can be at any position within the string.

unwrap(x, left, right = left, n_pairs = Inf)

unparens(x, n_pairs = Inf)

Arguments

x

character vector

left

left character to remove

right

right character to remove. Only removed if position is after left

n_pairs

number of character pairs to remove

Value

character vector with pairs removed

See also

Examples

# by default, removes all matching pairs of left and right x <- c("a", "(a)", "((a))", "(a) b", "a (b)", "(a) (b)" ) data.frame( x, unparens(x), check.names = FALSE )
#> x unparens(x) #> 1 a a #> 2 (a) a #> 3 ((a)) a #> 4 (a) b a b #> 5 a (b) a b #> 6 (a) (b) a b
# specify n_pairs to remove a specific number of pairs x <- c("(a)", "((a))", "(((a)))", "(a) (b)", "(a) (b) (c)", "(a) (b) (c) (d)") data.frame( x, "n_pairs=1" = unparens(x, n_pairs = 1), "n_pairs=2" = unparens(x, n_pairs = 2), "n_pairs=3" = unparens(x, n_pairs = 3), "n_pairs=Inf" = unparens(x), # the default check.names = FALSE )
#> x n_pairs=1 n_pairs=2 n_pairs=3 n_pairs=Inf #> 1 (a) a a a a #> 2 ((a)) (a) a a a #> 3 (((a))) ((a)) (a) a a #> 4 (a) (b) a (b) a b a b a b #> 5 (a) (b) (c) a (b) (c) a b (c) a b c a b c #> 6 (a) (b) (c) (d) a (b) (c) (d) a b (c) (d) a b c (d) a b c d
# use unwrap() to specify any pair of characters for left and right x <- "A string with some \\emph{latex tags}." unwrap(x, "\\emph{", "}")
#> [1] "A string with some latex tags."
# by default, only pairs are removed. Set a character to "" to override. x <- c("a)", "a))", "(a", "((a" ) data.frame(x, unparens(x), 'left=""' = unwrap(x, left = "", right = ")"), check.names = FALSE)
#> x unparens(x) left="" #> 1 a) a) a #> 2 a)) a)) a #> 3 (a (a (a #> 4 ((a ((a ((a
# make your own functions like this # markdown bold unbold <- function(x) unwrap(x, "**") bold <- function(...) wrap(paste(...), "**") (x <- (p("make a word", bold("bold"))))
#> [1] "make a word**bold**"
unbold(x)
#> [1] "make a wordbold"