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
).
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 instringi::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 ofstring
/replacement, so the total number of context characters is at the maximum2 * n_context_chrs
. Only relevant ifverbose = TRUE
.
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"))
#> 1× - Make love, not war
#> + Make hummus, not war
#> 1× - 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"))
#> 1× - Make love, not war
#> + Make hummus, not war
#> 1× - 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()
#> 1× - … are not dura…
#> + … are extremely dura…
#> 1× - not scar…
#> + extremely scar…
#> 1× - not prog…
#> + extremely prog…
#> 1× - not fung…
#> + extremely fung…
#> 1× - not veri…
#> + extremely veri…
#> 1× - not divi…
#> + extremely divi…
#> 1× - and hard to t…
#> + and ridiculously easy to t…
#> 1× - But tell me more about your analogy...
#> + So... flower power?
#> 1× - -[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()
#> 1× - …not…
#> + …extremely…
#> 5× - not…
#> + extremely…
#> 1× - …hard…
#> + …ridiculously easy…
#> 1× - But tell me more about your analogy...
#> + So... flower power?
#> 1× - -[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?
#>
#> -🤪