pattern
.stri_replace_all(str, replacement, ..., regex, fixed, coll, charclass)stri_replace_first(str, replacement, ..., regex, fixed, coll, charclass)
stri_replace_last(str, replacement, ..., regex, fixed, coll, charclass)
stri_replace(str, replacement, ..., regex, fixed, coll, charclass,
mode = c("first", "all", "last"))
stri_replace_all_charclass(str, pattern, replacement, merge = FALSE,
vectorize_all = TRUE)
stri_replace_first_charclass(str, pattern, replacement)
stri_replace_last_charclass(str, pattern, replacement)
stri_replace_all_coll(str, pattern, replacement, vectorize_all = TRUE,
opts_collator = NULL)
stri_replace_first_coll(str, pattern, replacement, opts_collator = NULL)
stri_replace_last_coll(str, pattern, replacement, opts_collator = NULL)
stri_replace_all_fixed(str, pattern, replacement, vectorize_all = TRUE)
stri_replace_first_fixed(str, pattern, replacement)
stri_replace_last_fixed(str, pattern, replacement)
stri_replace_all_regex(str, pattern, replacement, vectorize_all = TRUE,
opts_regex = NULL)
stri_replace_first_regex(str, pattern, replacement, opts_regex = NULL)
stri_replace_last_regex(str, pattern, replacement, opts_regex = NULL)
"first"
(the default), "all"
, "last"
stri_replace_all_charclass
onlystri_replace_all_*
onlystri_opts_collator
; NULL
for default settings; stri_replace_*_coll
onlystri_opts_regex
; NULL
for default settings;
stri_replace_*_regex
onlystr
, pattern
, replacement
. Then
these functions scan the input string for matches of the pattern.
Input that is not part of any match is left unchanged;
each match is replaced in the result by the replacement string.However, for stri_replace_all*
, if vectorize_all
is FALSE
,
the each substring matching any of the supplied pattern
s
is replaced by a corresponding replacement
string.
In such a case, the vectorization is over str
,
and - independently - over pattern
and replacement
.
In other words, this is equivalent to something like
for (i in 1:npatterns) str <- stri_replace_all(str, pattern[i], replacement[i]
.
Note that you must set length(pattern) >= length(replacement)
.
In case of stri_replace_*_regex
,
the replacement string may contain references to capture groups
(in round parentheses).
References are of the form $n
, where n
is the number
of the capture group (their numbering starts from 1).
In order to treat the $
character literally,
escape it with a backslash.
stri_replace
, stri_replace_all
, stri_replace_first
,
and stri_replace_last
are convenience functions.
They just call stri_replace_*_*
, depending on arguments used.
Unless you are a very lazy person, please call the underlying functions
directly for better performance.
If you would like to get rid of e.g. whitespaces from the start or end
of a string, see stri_trim
.
stri_replace_na
;
stri_trim
, stri_trim
,
stri_trim_both
,
stri_trim_left
,
stri_trim_right
;
stringi-search
stri_replace_all_charclass("aaaa", "[a]", "b", merge=c(TRUE, FALSE))
stri_replace_all_charclass("a\nb\tc d", "\\p{WHITE_SPACE}", " ")
stri_replace_all_charclass("a\nb\tc d", "\\p{WHITE_SPACE}", " ", merge=RUE)
s <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
stri_replace_all_fixed(s, " ", "#")
stri_replace_all_fixed(s, "o", "0")
stri_replace_all_regex(s, " .*? ", "#")
stri_replace_all_regex(s, "(el|s)it", "1234")
stri_replace_all_regex('abaca', 'a', c('!', '*'))
stri_replace_all_regex('123|456|789', '(\\p{N}).(\\p{N})', '$2-$1')
stri_replace_all_regex(c("stringi R", "REXAMINE", "123"), '( R|R.)', ' r ')
# Compare the results:
stri_replace_all_fixed("The quick brown fox jumped over the lazy dog.",
c("quick", "brown", "fox"), c("slow", "black", "bear"), vectorize_all=TRUE)
stri_replace_all_fixed("The quick brown fox jumped over the lazy dog.",
c("quick", "brown", "fox"), c("slow", "black", "bear"), vectorize_all=FALSE)
# Compare the results:
stri_replace_all_fixed("The quicker brown fox jumped over the lazy dog.",
c("quick", "brown", "fox"), c("slow", "black", "bear"), vectorize_all=FALSE)
stri_replace_all_regex("The quicker brown fox jumped over the lazy dog.",
"\\b"%s+%c("quick", "brown", "fox")%s+%"\\b", c("slow", "black", "bear"), vectorize_all=FALSE)
Run the code above in your browser using DataLab