str_replace_all {stringr}R Documentation

Replace all occurrences of a matched pattern in a string.

Description

Vectorised over string, pattern and replacement. Shorter arguments will be expanded to length of longest.

Usage

  str_replace_all(string, pattern, replacement)

Arguments

replacement

replacement string. References of the form \1, \2 will be replaced with the contents of the respective matched group (created by ()) within the pattern.

string

input vector. This must be an atomic vector, and will be coerced to a character vector

pattern

pattern to look for, as defined by a POSIX regular expression. See the “Extended Regular Expressions” section of regex for details. See fixed, ignore.case and perl for how to use other types of matching: fixed, case insensitive and perl-compatible.

Value

character vector.

See Also

gsub which this function wraps, str_replace_all to replace a single match

Examples

fruits <- c("one apple", "two pears", "three bananas")
str_replace(fruits, "[aeiou]", "-")
str_replace_all(fruits, "[aeiou]", "-")

str_replace_all(fruits, "([aeiou])", "")
str_replace_all(fruits, "([aeiou])", "\\1\\1")
str_replace_all(fruits, "[aeiou]", c("1", "2", "3"))
str_replace_all(fruits, c("a", "e", "i"), "-")

[Package stringr version 0.6 Index]