Skip to contents

Applies a series of regular-expression-replacement pairs to one or more strings. All performed replacements are displayed on the console by default (verbose = TRUE).

Usage

str_replace_verbose(string, pattern, verbose = TRUE, n_context_chrs = 20L)

Arguments

string

Input vector. Either a character vector, or something coercible to one.

pattern

A named character vector with patterns as names and replacements as values (c(pattern1 = replacement1)). Patterns are interpreted as regular expressions as described in stringi::stringi-search-regex(). Replacements are interpreted as-is, except that references of the form \1, \2, etc. will be replaced with the contents of the respective matched group (created in patterns using ()). Pattern-replacement pairs are processed in the order given, meaning that first listed pairs are applied before later listed ones.

verbose

Whether or not to display replacements on the console.

n_context_chrs

The (maximum) number of characters displayed around the actual string and its replacement. The number refers to a single side of string/replacement, so the total number of context characters is at the maximum 2 * n_context_chrs. Only relevant if verbose = TRUE.

Value

A character vector.

Details

This function provides a subset of stringr::str_replace_all()'s functionality. If you don't need the visual console output, it's recommended to directly resort to the latter.

See also

Other string functions: str_normalize(), str_normalize_file(), str_replace_file()

Examples

"Make love, not war" |>
  yay::str_replace_verbose(pattern = c("love" = "hummus",
                                       "war" = "walls"))
#>- Make love, not war
#>    + Make hummus, not war
#>- Make hummus, not war
#>    + Make hummus, not walls
#> [1] "Make hummus, not walls"

# pattern-replacement pairs are processed one-by-one, so the following gives the same result
"Make love, not war" |>
  yay::str_replace_verbose(pattern = c("love" = "hummus",
                                       "hummus, not war" = "hummus, not walls"))
#>- Make love, not war
#>    + Make hummus, not war
#>- Make hummus, not war
#>    + Make hummus, not walls
#> [1] "Make hummus, not walls"

# varying `n_context_chrs` affects console output summarization
input <- c("Tulips are not durable, ",
           "not scarce, ",
           "not programmable, ",
           "not fungible, ",
           "not verifiable, ",
           "not divisible, ",
           "and hard to transfer. ",
           "But tell me more about your analogy...",
           "",
           "-[Naval Ravikant](https://twitter.com/naval/status/939316447318122496)")

pattern <- c("not" = "extremely",
             "hard" = "ridiculously easy",
             "^But.*" = "So... flower power?",
             "(^-).*Naval.*" = "\\1\U0001F92A")

yay::str_replace_verbose(string = input,
                         pattern = pattern,
                         n_context_chrs = 5L) |>
  pal::cat_lines()
#>- … are not dura…
#>    + … are extremely dura…
#>- not scar…
#>    + extremely scar…
#>- not prog…
#>    + extremely prog…
#>- not fung…
#>    + extremely fung…
#>- not veri…
#>    + extremely veri…
#>- not divi…
#>    + extremely divi…
#>- and hard to t…
#>    + and ridiculously easy to t…
#>- But tell me more about your analogy...
#>    + So... flower power?
#>- -[Naval Ravikant](https://twitter.com/naval/status/939316447318122496)
#>    + \1🤪
#> Tulips are extremely durable, 
#> extremely scarce, 
#> extremely programmable, 
#> extremely fungible, 
#> extremely verifiable, 
#> extremely divisible, 
#> and ridiculously easy to transfer. 
#> So... flower power?
#> 
#> -🤪

yay::str_replace_verbose(string = input,
                         pattern = pattern,
                         n_context_chrs = 0L) |>
  pal::cat_lines()
#>- not
#>    + extremely
#>- not
#>    + extremely
#>- hard
#>    + ridiculously easy
#>- But tell me more about your analogy...
#>    + So... flower power?
#>- -[Naval Ravikant](https://twitter.com/naval/status/939316447318122496)
#>    + \1🤪
#> Tulips are extremely durable, 
#> extremely scarce, 
#> extremely programmable, 
#> extremely fungible, 
#> extremely verifiable, 
#> extremely divisible, 
#> and ridiculously easy to transfer. 
#> So... flower power?
#> 
#> -🤪