Replace part of a character string.

DESCRIPTION:

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

USAGE:

substituteString(pattern, replacement, x, ignore.case=FALSE, extended=TRUE, 
   perl=FALSE, global=FALSE, fixed=FALSE)
gsub(pattern, replacement, x, ignore.case=FALSE, extended=TRUE, 
   perl=FALSE, fixed=FALSE)
sub(pattern, replacement, x, ignore.case=FALSE, extended=TRUE, 
   perl=FALSE, fixed=FALSE)

REQUIRED 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 not TRUE, then you may use "\\n" in the replacement string (where n is a digit) to mean to insert the n'th parenthesized subpattern here. Because the backslash may have a special meaning in replacements when fixed is FALSE, you must use 2 backslashes to put a backslash in the replacement. (Because Spotfire S+ uses 2 backslashes to represent 1 backslash, that means you must type 4 backslashes.)
x
A vector of strings in which to do the replacement.

OPTIONAL ARGUMENTS:

ignore.case
If TRUE then the case (upper or lower) of alphabetic characters is ignored so that, e.g., the pattern "A" matches both "a" and "A".
extended
If TRUE the pattern is taken to be an extended regular expression. Otherwise it is interpreted as an obsolete regular expression. See the help file for regexpr for details.
perl
This argument is currently ignored and is treated as FALSE.
global
If FALSE only the first match of pattern in x will be replaced. If TRUE then all matches will be replaced.
fixed
If TRUE the pattern is not interpreted as a regular expression, but as a string with no special characters in it. In this case the extended and perl are taken to be FALSE, but the ignore.case is respected.

VALUE:

A vector of altered strings as long as x.

DETAILS:

For compatibility with R, the gsub and sub functions are the same as substituteString with global=TRUE and global=FALSE, respectively. Versions of S-PLUS prior to 8.0 had a sub function for subscripting 2 dimensional objects. It has been renamed subscript2d .

SEE ALSO:

, .

EXAMPLES:

# Remove possible .xls suffix, any case
substituteString("\\.xls$", "", c("Data.XLS", "Odd.xls.XLS"), ignore.case=TRUE)
# [1] "Data"    "Odd.xls"
substituteString("man", "MAN", "A man, another man, and a woman", global=TRUE)
# [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/TIBCO/splus81\\library\\bigdata"), "\n")
# C:\PROGRAM FILES\TIBCO\splus81\library\bigdata