gsub
Replace Part of a Character String

Description

Replaces the part(s) of a string matching a regular expression pattern.

Usage

gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE, 
    fixed = FALSE, useBytes = FALSE) 
sub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE, 
    fixed = FALSE, useBytes = FALSE) 

Arguments

pattern a regular expression pattern as used by regexpr.
replacement the string (or vector of strings) to replace the parts of x matching the pattern. If fixed is FALSE (the default), then you can use "\n" in the replacement string (where n is a digit) to mean to insert the n-th parenthesized subpattern here.

If perl is TRUE, the replacement string can contain backslash followed by "U", "L", or "E", specifying that following "\n" insertions should be converted to uppercase, lowercase, or inserted without changing their case.

Because the backslash might have a special meaning in replacements when fixed is FALSE, you must use 2 backslashes to put a backslash character in the replacement. Because a string constant uses 2 backslashes to represent 1 backslash, that means you must type 4 backslashes to add a single backslash character in the replacement.

x a vector of strings in which to do the replacement.
ignore.case a logical value. If TRUE, the case (upper or lower) of alphabetic characters is ignored so that, for example, the pattern "A" matches both "a" and "A". The default is FALSE.
perl a logical value. See regexpr for information about how it affects the intepretation of the pattern argument.
fixed a logical value. If FALSE (the default, the pattern is interpreted as a regular expression. If TRUE the pattern is interpreted as a string containing no special characters.
useBytes a logical value. If TRUE then the string arguments are treated as a simple sequence of bytes. If this is FALSE and any of the x or pattern or replacement strings have 'bytes' encoding (see Encoding), then useBytes is set to TRUE.

Details

Each element of the return value will have the same string encoding as the corresponding element of x, unless the substituted characters cannot be represented in this encoding, in which case the output element will have the 'UTF-8' encoding.
Value
returns a vector of altered strings as long as x.
gsuball matches are replaced.
subonly the first match is replaced.
See Also
substring, regexpr, Encoding.
Examples
# Remove possible .xls suffix, any case
sub("\\.xls$", "", c("Data.XLS", "Odd.xls.XLS"), ignore.case=TRUE)
# [1] "Data"    "Odd.xls"

gsub("man", "MAN", "A man, another man, and a woman") # [1] "A MAN, another MAN, and a woMAN"

gsub("([-+]?[0-9]+)", "<INTEGER:\\1>", "10 packs of 6") # [1] "<INTEGER:10> packs of <INTEGER:6>"

cat(gsub("[\\/]", "\\\\", "C:/PROGRAM FILES\\Spotfire"), "\n") # C:\PROGRAM FILES\Spotfire

Package base version 6.1.4-13
Package Index