EVOLUTION-MANAGER
Edit File: stri_replace.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Replace Occurrences of a Pattern</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for stri_replace_all {stringi}"><tr><td>stri_replace_all {stringi}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Replace Occurrences of a Pattern</h2> <h3>Description</h3> <p>These functions replace, with the given replacement string, every/first/last substring of the input that matches the specified <code>pattern</code>. </p> <h3>Usage</h3> <pre> 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, ..., opts_fixed = NULL ) stri_replace_first_fixed(str, pattern, replacement, ..., opts_fixed = NULL) stri_replace_last_fixed(str, pattern, replacement, ..., opts_fixed = NULL) 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) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>str</code></td> <td> <p>character vector; strings to search in</p> </td></tr> <tr valign="top"><td><code>replacement</code></td> <td> <p>character vector with replacements for matched patterns</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>supplementary arguments passed to the underlying functions, including additional settings for <code>opts_collator</code>, <code>opts_regex</code>, <code>opts_fixed</code>, and so on</p> </td></tr> <tr valign="top"><td><code>mode</code></td> <td> <p>single string; one of: <code>"first"</code> (the default), <code>"all"</code>, <code>"last"</code></p> </td></tr> <tr valign="top"><td><code>pattern, regex, fixed, coll, charclass</code></td> <td> <p>character vector; search patterns; for more details refer to <a href="stringi-search.html">stringi-search</a></p> </td></tr> <tr valign="top"><td><code>merge</code></td> <td> <p>single logical value; should consecutive matches be merged into one string; <code>stri_replace_all_charclass</code> only</p> </td></tr> <tr valign="top"><td><code>vectorize_all</code></td> <td> <p>single logical value; should each occurrence of a pattern in every string be replaced by a corresponding replacement string?; <code>stri_replace_all_*</code> only</p> </td></tr> <tr valign="top"><td><code>opts_collator, opts_fixed, opts_regex</code></td> <td> <p>a named list used to tune up the search engine's settings; see <code><a href="stri_opts_collator.html">stri_opts_collator</a></code>, <code><a href="stri_opts_fixed.html">stri_opts_fixed</a></code>, and <code><a href="stri_opts_regex.html">stri_opts_regex</a></code>, respectively; <code>NULL</code> for the defaults</p> </td></tr> </table> <h3>Details</h3> <p>By default, all the functions are vectorized over <code>str</code>, <code>pattern</code>, <code>replacement</code> (with recycling of the elements in the shorter vector if necessary). Input that is not part of any match is left unchanged; each match is replaced in the result by the replacement string. </p> <p>However, for <code>stri_replace_all*</code>, if <code>vectorize_all</code> is <code>FALSE</code>, the each substring matching any of the supplied <code>pattern</code>s is replaced by a corresponding <code>replacement</code> string. In such a case, the vectorization is over <code>str</code>, and - independently - over <code>pattern</code> and <code>replacement</code>. In other words, this is equivalent to something like <code>for (i in 1:npatterns) str <- stri_replace_all(str, pattern[i], replacement[i]</code>. Note that you must set <code>length(pattern) >= length(replacement)</code>. </p> <p>In case of <code>stri_replace_*_regex</code>, the replacement string may contain references to capture groups (in round parentheses). References are of the form <code>$n</code>, where <code>n</code> is the number of the capture group (<code>$1</code> denotes the first group). For the literal <code>$</code>, escape it with a backslash. Moreover, <code>${name}</code> are used for named capture groups. </p> <p><code>stri_replace</code>, <code>stri_replace_all</code>, <code>stri_replace_first</code>, and <code>stri_replace_last</code> are convenience functions; they just call <code>stri_replace_*_*</code> variants, depending on the arguments used. </p> <p>If you wish to remove white-spaces from the start or end of a string, see <code><a href="stri_trim.html">stri_trim</a></code>. </p> <h3>Value</h3> <p>All the functions return a character vector. </p> <h3>See Also</h3> <p>Other search_replace: <code><a href="stri_trim.html">stri_trim_both</a>()</code>, <code><a href="stringi-search.html">stringi-search</a></code> </p> <h3>Examples</h3> <pre> 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=TRUE) s <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit." stri_replace_all_fixed(s, " ", "#") stri_replace_all_fixed(s, "o", "0") stri_replace_all_fixed(c("1", "NULL", "3"), "NULL", NA) 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 ') ## Not run: # named capture groups available since ICU 55 stri_replace_all_regex("words 123 and numbers 456", "(?<numbers>[0-9]+)", "!${numbers}!") ## End(Not run) # 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) </pre> <hr /><div style="text-align: center;">[Package <em>stringi</em> version 1.4.6 <a href="00Index.html">Index</a>]</div> </body></html>